• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
8 #include "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkFont.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"
22 #include "include/effects/SkGradientShader.h"
23 
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.
27 static const SkColor COLORS[] = { SK_ColorRED, SK_ColorWHITE, SK_ColorBLUE,
28                                   SK_ColorBLACK, SK_ColorGREEN };
29 static const SkScalar POS[] = { 0.0, 0.0, 0.5, 1.0, 1.0 };
30 static const int COLOR_CT = SK_ARRAY_COUNT(COLORS);
31 
32 static const SkTileMode TILE_MODES[] = { SkTileMode::kDecal,
33                                          SkTileMode::kRepeat,
34                                          SkTileMode::kMirror,
35                                          SkTileMode::kClamp };
36 static const char* TILE_NAMES[] = { "decal", "repeat", "mirror", "clamp" };
37 static const int TILE_MODE_CT = SK_ARRAY_COUNT(TILE_MODES);
38 
39 static constexpr int TILE_SIZE = 100;
40 static constexpr int TILE_GAP = 10;
41 
42 static const SkPoint CENTER = SkPoint::Make(TILE_SIZE / 2, TILE_SIZE / 2);
43 
44 typedef sk_sp<SkShader> (*GradientFactory)(SkTileMode tm);
45 
draw_tile_header(SkCanvas * canvas)46 static void draw_tile_header(SkCanvas* canvas) {
47     canvas->save();
48 
49     for (int i = 0; i < TILE_MODE_CT; ++i) {
50         canvas->drawString(TILE_NAMES[i], 0, 0, SkFont(), SkPaint());
51         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 
draw_row(SkCanvas * canvas,const char * desc,GradientFactory factory)60 static void draw_row(SkCanvas* canvas, const char* desc, GradientFactory factory) {
61     canvas->save();
62 
63     SkPaint text;
64     text.setAntiAlias(true);
65 
66     canvas->translate(0, TILE_GAP);
67     canvas->drawString(desc, 0, 0, SkFont(), text);
68     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 
make_linear(SkTileMode mode)87 static sk_sp<SkShader> make_linear(SkTileMode mode) {
88     // Same position
89     SkPoint pts[2] = {CENTER, CENTER};
90     return SkGradientShader::MakeLinear(pts, COLORS, POS, COLOR_CT, mode);
91 }
92 
make_radial(SkTileMode mode)93 static sk_sp<SkShader> make_radial(SkTileMode mode) {
94     // Radius = 0
95     return SkGradientShader::MakeRadial(CENTER, 0.0, COLORS, POS, COLOR_CT, mode);
96 }
97 
make_sweep(SkTileMode mode)98 static sk_sp<SkShader> make_sweep(SkTileMode mode) {
99     // 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 
make_sweep_zero_ang(SkTileMode mode)105 static sk_sp<SkShader> make_sweep_zero_ang(SkTileMode mode) {
106     // 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 
make_2pt_conic(SkTileMode mode)111 static sk_sp<SkShader> make_2pt_conic(SkTileMode mode) {
112     // 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 
make_2pt_conic_zero_rad(SkTileMode mode)117 static sk_sp<SkShader> make_2pt_conic_zero_rad(SkTileMode mode) {
118     // 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 
123 class DegenerateGradientGM : public skiagm::GM {
124 public:
DegenerateGradientGM()125     DegenerateGradientGM() {
126 
127     }
128 
129 protected:
onShortName()130     SkString onShortName() override {
131         return SkString("degenerate_gradients");
132     }
133 
onISize()134     SkISize onISize() override {
135         return SkISize::Make(800, 800);
136     }
137 
onDraw(SkCanvas * canvas)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 
152 private:
153     typedef skiagm::GM INHERITED;
154 };
155 
156 DEF_GM(return new DegenerateGradientGM;)
157