blob: 6ac5a6369c48d1f77893663c2481e053e2542f6d [file] [log] [blame]
Mike Klein024072a2018-11-11 00:26:30 +00001/*
2 * Copyright 2018 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
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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkFont.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#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"
Robert Phillips3d311a92020-05-05 09:18:50 -040023#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040024#include "tools/fonts/FontToolUtils.h"
Mike Klein024072a2018-11-11 00:26:30 +000025
26// NOTE: The positions define hardstops for the red and green borders. For the repeating degenerate
27// gradients, that means the red and green are never visible, so the average color used should only
28// be based off of the white, blue, black blend.
29static const SkColor COLORS[] = { SK_ColorRED, SK_ColorWHITE, SK_ColorBLUE,
30 SK_ColorBLACK, SK_ColorGREEN };
31static const SkScalar POS[] = { 0.0, 0.0, 0.5, 1.0, 1.0 };
Herb Derbyc37b3862022-06-21 09:49:17 -040032static const int COLOR_CT = std::size(COLORS);
Mike Klein024072a2018-11-11 00:26:30 +000033
Mike Reedfae8fce2019-04-03 10:27:45 -040034static const SkTileMode TILE_MODES[] = { SkTileMode::kDecal,
35 SkTileMode::kRepeat,
36 SkTileMode::kMirror,
37 SkTileMode::kClamp };
Mike Klein024072a2018-11-11 00:26:30 +000038static const char* TILE_NAMES[] = { "decal", "repeat", "mirror", "clamp" };
Herb Derbyc37b3862022-06-21 09:49:17 -040039static const int TILE_MODE_CT = std::size(TILE_MODES);
Mike Klein024072a2018-11-11 00:26:30 +000040
41static constexpr int TILE_SIZE = 100;
42static constexpr int TILE_GAP = 10;
43
44static const SkPoint CENTER = SkPoint::Make(TILE_SIZE / 2, TILE_SIZE / 2);
45
Mike Reedfae8fce2019-04-03 10:27:45 -040046typedef sk_sp<SkShader> (*GradientFactory)(SkTileMode tm);
Mike Klein024072a2018-11-11 00:26:30 +000047
48static void draw_tile_header(SkCanvas* canvas) {
49 canvas->save();
50
Kevin Lubicke836c3a2023-10-20 06:55:35 -040051 SkFont font(ToolUtils::DefaultPortableTypeface(), 12);
Mike Klein024072a2018-11-11 00:26:30 +000052 for (int i = 0; i < TILE_MODE_CT; ++i) {
Robert Phillips3d311a92020-05-05 09:18:50 -040053 canvas->drawString(TILE_NAMES[i], 0, 0, font, SkPaint());
Mike Klein024072a2018-11-11 00:26:30 +000054 canvas->translate(TILE_SIZE + TILE_GAP, 0);
55 }
56
57 canvas->restore();
58
59 // Now adjust to start at rows below the header
60 canvas->translate(0, 2 * TILE_GAP);
61}
62
63static void draw_row(SkCanvas* canvas, const char* desc, GradientFactory factory) {
64 canvas->save();
65
66 SkPaint text;
Mike Klein024072a2018-11-11 00:26:30 +000067 text.setAntiAlias(true);
68
Kevin Lubicke836c3a2023-10-20 06:55:35 -040069 SkFont font(ToolUtils::DefaultPortableTypeface(), 12);
Robert Phillips3d311a92020-05-05 09:18:50 -040070
Mike Klein024072a2018-11-11 00:26:30 +000071 canvas->translate(0, TILE_GAP);
Robert Phillips3d311a92020-05-05 09:18:50 -040072 canvas->drawString(desc, 0, 0, font, text);
Mike Klein024072a2018-11-11 00:26:30 +000073 canvas->translate(0, TILE_GAP);
74
75 SkPaint paint;
76 paint.setColor(SK_ColorBLACK);
77 paint.setStyle(SkPaint::kStrokeAndFill_Style);
78 paint.setStrokeWidth(2.0f);
79
80 for (int i = 0; i < TILE_MODE_CT; ++i) {
81 paint.setShader(factory(TILE_MODES[i]));
82 canvas->drawRect(SkRect::MakeWH(TILE_SIZE, TILE_SIZE), paint);
83 canvas->translate(TILE_SIZE + TILE_GAP, 0);
84 }
85
86 canvas->restore();
87
88 // Now adjust to start the next row below this one (1 gap for text and 2 gap for margin)
89 canvas->translate(0, 3 * TILE_GAP + TILE_SIZE);
90}
91
Mike Reedfae8fce2019-04-03 10:27:45 -040092static sk_sp<SkShader> make_linear(SkTileMode mode) {
Mike Klein024072a2018-11-11 00:26:30 +000093 // Same position
94 SkPoint pts[2] = {CENTER, CENTER};
95 return SkGradientShader::MakeLinear(pts, COLORS, POS, COLOR_CT, mode);
96}
97
Mike Reedfae8fce2019-04-03 10:27:45 -040098static sk_sp<SkShader> make_radial(SkTileMode mode) {
Mike Klein024072a2018-11-11 00:26:30 +000099 // Radius = 0
100 return SkGradientShader::MakeRadial(CENTER, 0.0, COLORS, POS, COLOR_CT, mode);
101}
102
Mike Reedfae8fce2019-04-03 10:27:45 -0400103static sk_sp<SkShader> make_sweep(SkTileMode mode) {
Mike Klein024072a2018-11-11 00:26:30 +0000104 // Start and end angles at 45
105 static constexpr SkScalar SWEEP_ANG = 45.0;
106 return SkGradientShader::MakeSweep(CENTER.fX, CENTER.fY, COLORS, POS, COLOR_CT, mode,
107 SWEEP_ANG, SWEEP_ANG, 0, nullptr);
108}
109
Mike Reedfae8fce2019-04-03 10:27:45 -0400110static sk_sp<SkShader> make_sweep_zero_ang(SkTileMode mode) {
Mike Klein024072a2018-11-11 00:26:30 +0000111 // Start and end angles at 0
112 return SkGradientShader::MakeSweep(CENTER.fX, CENTER.fY, COLORS, POS, COLOR_CT, mode,
113 0.0, 0.0, 0, nullptr);
114}
115
Mike Reedfae8fce2019-04-03 10:27:45 -0400116static sk_sp<SkShader> make_2pt_conic(SkTileMode mode) {
Mike Klein024072a2018-11-11 00:26:30 +0000117 // Start and end radius = TILE_SIZE, same position
118 return SkGradientShader::MakeTwoPointConical(CENTER, TILE_SIZE / 2, CENTER, TILE_SIZE / 2,
119 COLORS, POS, COLOR_CT, mode);
120}
121
Mike Reedfae8fce2019-04-03 10:27:45 -0400122static sk_sp<SkShader> make_2pt_conic_zero_rad(SkTileMode mode) {
Mike Klein024072a2018-11-11 00:26:30 +0000123 // Start and end radius = 0, same position
124 return SkGradientShader::MakeTwoPointConical(CENTER, 0.0, CENTER, 0.0, COLORS, POS,
125 COLOR_CT, mode);
126}
127
128class DegenerateGradientGM : public skiagm::GM {
129public:
130 DegenerateGradientGM() {
131
132 }
133
134protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000135 SkString getName() const override { return SkString("degenerate_gradients"); }
Mike Klein024072a2018-11-11 00:26:30 +0000136
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000137 SkISize getISize() override { return SkISize::Make(800, 800); }
Mike Klein024072a2018-11-11 00:26:30 +0000138
139 void onDraw(SkCanvas* canvas) override {
140 canvas->translate(3 * TILE_GAP, 3 * TILE_GAP);
141 draw_tile_header(canvas);
142
143 draw_row(canvas, "linear: empty, blue, blue, green", make_linear);
144 draw_row(canvas, "radial: empty, blue, blue, green", make_radial);
145 draw_row(canvas, "sweep-0: empty, blue, blue, green", make_sweep_zero_ang);
146 draw_row(canvas, "sweep-45: empty, blue, blue, red 45 degree sector then green",
147 make_sweep);
148 draw_row(canvas, "2pt-conic-0: empty, blue, blue, green", make_2pt_conic_zero_rad);
149 draw_row(canvas, "2pt-conic-1: empty, blue, blue, full red circle on green",
150 make_2pt_conic);
151 }
152
153private:
John Stiles7571f9e2020-09-02 22:42:33 -0400154 using INHERITED = skiagm::GM;
Mike Klein024072a2018-11-11 00:26:30 +0000155};
156
157DEF_GM(return new DegenerateGradientGM;)