blob: 8fea72e21a9a0784458369135b79da774eb34af2 [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"
Mike Klein024072a2018-11-11 00:26:30 +000023
24// NOTE: The positions define hardstops for the red and green borders. For the repeating degenerate
25// gradients, that means the red and green are never visible, so the average color used should only
26// be based off of the white, blue, black blend.
27static const SkColor COLORS[] = { SK_ColorRED, SK_ColorWHITE, SK_ColorBLUE,
28 SK_ColorBLACK, SK_ColorGREEN };
29static const SkScalar POS[] = { 0.0, 0.0, 0.5, 1.0, 1.0 };
30static const int COLOR_CT = SK_ARRAY_COUNT(COLORS);
31
Mike Reedfae8fce2019-04-03 10:27:45 -040032static const SkTileMode TILE_MODES[] = { SkTileMode::kDecal,
33 SkTileMode::kRepeat,
34 SkTileMode::kMirror,
35 SkTileMode::kClamp };
Mike Klein024072a2018-11-11 00:26:30 +000036static const char* TILE_NAMES[] = { "decal", "repeat", "mirror", "clamp" };
37static const int TILE_MODE_CT = SK_ARRAY_COUNT(TILE_MODES);
38
39static constexpr int TILE_SIZE = 100;
40static constexpr int TILE_GAP = 10;
41
42static const SkPoint CENTER = SkPoint::Make(TILE_SIZE / 2, TILE_SIZE / 2);
43
Mike Reedfae8fce2019-04-03 10:27:45 -040044typedef sk_sp<SkShader> (*GradientFactory)(SkTileMode tm);
Mike Klein024072a2018-11-11 00:26:30 +000045
46static void draw_tile_header(SkCanvas* canvas) {
47 canvas->save();
48
Mike Klein024072a2018-11-11 00:26:30 +000049 for (int i = 0; i < TILE_MODE_CT; ++i) {
Mike Reedc4745d62019-01-07 09:31:58 -050050 canvas->drawString(TILE_NAMES[i], 0, 0, SkFont(), SkPaint());
Mike Klein024072a2018-11-11 00:26:30 +000051 canvas->translate(TILE_SIZE + TILE_GAP, 0);
52 }
53
54 canvas->restore();
55
56 // Now adjust to start at rows below the header
57 canvas->translate(0, 2 * TILE_GAP);
58}
59
60static void draw_row(SkCanvas* canvas, const char* desc, GradientFactory factory) {
61 canvas->save();
62
63 SkPaint text;
Mike Klein024072a2018-11-11 00:26:30 +000064 text.setAntiAlias(true);
65
66 canvas->translate(0, TILE_GAP);
Mike Reed1af9b482019-01-07 11:01:57 -050067 canvas->drawString(desc, 0, 0, SkFont(), text);
Mike Klein024072a2018-11-11 00:26:30 +000068 canvas->translate(0, TILE_GAP);
69
70 SkPaint paint;
71 paint.setColor(SK_ColorBLACK);
72 paint.setStyle(SkPaint::kStrokeAndFill_Style);
73 paint.setStrokeWidth(2.0f);
74
75 for (int i = 0; i < TILE_MODE_CT; ++i) {
76 paint.setShader(factory(TILE_MODES[i]));
77 canvas->drawRect(SkRect::MakeWH(TILE_SIZE, TILE_SIZE), paint);
78 canvas->translate(TILE_SIZE + TILE_GAP, 0);
79 }
80
81 canvas->restore();
82
83 // Now adjust to start the next row below this one (1 gap for text and 2 gap for margin)
84 canvas->translate(0, 3 * TILE_GAP + TILE_SIZE);
85}
86
Mike Reedfae8fce2019-04-03 10:27:45 -040087static sk_sp<SkShader> make_linear(SkTileMode mode) {
Mike Klein024072a2018-11-11 00:26:30 +000088 // Same position
89 SkPoint pts[2] = {CENTER, CENTER};
90 return SkGradientShader::MakeLinear(pts, COLORS, POS, COLOR_CT, mode);
91}
92
Mike Reedfae8fce2019-04-03 10:27:45 -040093static sk_sp<SkShader> make_radial(SkTileMode mode) {
Mike Klein024072a2018-11-11 00:26:30 +000094 // Radius = 0
95 return SkGradientShader::MakeRadial(CENTER, 0.0, COLORS, POS, COLOR_CT, mode);
96}
97
Mike Reedfae8fce2019-04-03 10:27:45 -040098static sk_sp<SkShader> make_sweep(SkTileMode mode) {
Mike Klein024072a2018-11-11 00:26:30 +000099 // Start and end angles at 45
100 static constexpr SkScalar SWEEP_ANG = 45.0;
101 return SkGradientShader::MakeSweep(CENTER.fX, CENTER.fY, COLORS, POS, COLOR_CT, mode,
102 SWEEP_ANG, SWEEP_ANG, 0, nullptr);
103}
104
Mike Reedfae8fce2019-04-03 10:27:45 -0400105static sk_sp<SkShader> make_sweep_zero_ang(SkTileMode mode) {
Mike Klein024072a2018-11-11 00:26:30 +0000106 // Start and end angles at 0
107 return SkGradientShader::MakeSweep(CENTER.fX, CENTER.fY, COLORS, POS, COLOR_CT, mode,
108 0.0, 0.0, 0, nullptr);
109}
110
Mike Reedfae8fce2019-04-03 10:27:45 -0400111static sk_sp<SkShader> make_2pt_conic(SkTileMode mode) {
Mike Klein024072a2018-11-11 00:26:30 +0000112 // Start and end radius = TILE_SIZE, same position
113 return SkGradientShader::MakeTwoPointConical(CENTER, TILE_SIZE / 2, CENTER, TILE_SIZE / 2,
114 COLORS, POS, COLOR_CT, mode);
115}
116
Mike Reedfae8fce2019-04-03 10:27:45 -0400117static sk_sp<SkShader> make_2pt_conic_zero_rad(SkTileMode mode) {
Mike Klein024072a2018-11-11 00:26:30 +0000118 // Start and end radius = 0, same position
119 return SkGradientShader::MakeTwoPointConical(CENTER, 0.0, CENTER, 0.0, COLORS, POS,
120 COLOR_CT, mode);
121}
122
123class DegenerateGradientGM : public skiagm::GM {
124public:
125 DegenerateGradientGM() {
126
127 }
128
129protected:
130 SkString onShortName() override {
131 return SkString("degenerate_gradients");
132 }
133
134 SkISize onISize() override {
135 return SkISize::Make(800, 800);
136 }
137
138 void onDraw(SkCanvas* canvas) override {
139 canvas->translate(3 * TILE_GAP, 3 * TILE_GAP);
140 draw_tile_header(canvas);
141
142 draw_row(canvas, "linear: empty, blue, blue, green", make_linear);
143 draw_row(canvas, "radial: empty, blue, blue, green", make_radial);
144 draw_row(canvas, "sweep-0: empty, blue, blue, green", make_sweep_zero_ang);
145 draw_row(canvas, "sweep-45: empty, blue, blue, red 45 degree sector then green",
146 make_sweep);
147 draw_row(canvas, "2pt-conic-0: empty, blue, blue, green", make_2pt_conic_zero_rad);
148 draw_row(canvas, "2pt-conic-1: empty, blue, blue, full red circle on green",
149 make_2pt_conic);
150 }
151
152private:
153 typedef skiagm::GM INHERITED;
154};
155
156DEF_GM(return new DegenerateGradientGM;)