Mike Klein | 024072a | 2018-11-11 00:26:30 +0000 | [diff] [blame] | 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkColor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkFont.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 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" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "include/effects/SkGradientShader.h" |
Mike Klein | 024072a | 2018-11-11 00:26:30 +0000 | [diff] [blame] | 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 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 32 | static const SkTileMode TILE_MODES[] = { SkTileMode::kDecal, |
| 33 | SkTileMode::kRepeat, |
| 34 | SkTileMode::kMirror, |
| 35 | SkTileMode::kClamp }; |
Mike Klein | 024072a | 2018-11-11 00:26:30 +0000 | [diff] [blame] | 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 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 44 | typedef sk_sp<SkShader> (*GradientFactory)(SkTileMode tm); |
Mike Klein | 024072a | 2018-11-11 00:26:30 +0000 | [diff] [blame] | 45 | |
| 46 | static void draw_tile_header(SkCanvas* canvas) { |
| 47 | canvas->save(); |
| 48 | |
Mike Klein | 024072a | 2018-11-11 00:26:30 +0000 | [diff] [blame] | 49 | for (int i = 0; i < TILE_MODE_CT; ++i) { |
Mike Reed | c4745d6 | 2019-01-07 09:31:58 -0500 | [diff] [blame] | 50 | canvas->drawString(TILE_NAMES[i], 0, 0, SkFont(), SkPaint()); |
Mike Klein | 024072a | 2018-11-11 00:26:30 +0000 | [diff] [blame] | 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 | |
| 60 | static void draw_row(SkCanvas* canvas, const char* desc, GradientFactory factory) { |
| 61 | canvas->save(); |
| 62 | |
| 63 | SkPaint text; |
Mike Klein | 024072a | 2018-11-11 00:26:30 +0000 | [diff] [blame] | 64 | text.setAntiAlias(true); |
| 65 | |
| 66 | canvas->translate(0, TILE_GAP); |
Mike Reed | 1af9b48 | 2019-01-07 11:01:57 -0500 | [diff] [blame] | 67 | canvas->drawString(desc, 0, 0, SkFont(), text); |
Mike Klein | 024072a | 2018-11-11 00:26:30 +0000 | [diff] [blame] | 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 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 87 | static sk_sp<SkShader> make_linear(SkTileMode mode) { |
Mike Klein | 024072a | 2018-11-11 00:26:30 +0000 | [diff] [blame] | 88 | // Same position |
| 89 | SkPoint pts[2] = {CENTER, CENTER}; |
| 90 | return SkGradientShader::MakeLinear(pts, COLORS, POS, COLOR_CT, mode); |
| 91 | } |
| 92 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 93 | static sk_sp<SkShader> make_radial(SkTileMode mode) { |
Mike Klein | 024072a | 2018-11-11 00:26:30 +0000 | [diff] [blame] | 94 | // Radius = 0 |
| 95 | return SkGradientShader::MakeRadial(CENTER, 0.0, COLORS, POS, COLOR_CT, mode); |
| 96 | } |
| 97 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 98 | static sk_sp<SkShader> make_sweep(SkTileMode mode) { |
Mike Klein | 024072a | 2018-11-11 00:26:30 +0000 | [diff] [blame] | 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 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 105 | static sk_sp<SkShader> make_sweep_zero_ang(SkTileMode mode) { |
Mike Klein | 024072a | 2018-11-11 00:26:30 +0000 | [diff] [blame] | 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 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 111 | static sk_sp<SkShader> make_2pt_conic(SkTileMode mode) { |
Mike Klein | 024072a | 2018-11-11 00:26:30 +0000 | [diff] [blame] | 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 | |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 117 | static sk_sp<SkShader> make_2pt_conic_zero_rad(SkTileMode mode) { |
Mike Klein | 024072a | 2018-11-11 00:26:30 +0000 | [diff] [blame] | 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: |
| 125 | DegenerateGradientGM() { |
| 126 | |
| 127 | } |
| 128 | |
| 129 | protected: |
| 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 | |
| 152 | private: |
| 153 | typedef skiagm::GM INHERITED; |
| 154 | }; |
| 155 | |
| 156 | DEF_GM(return new DegenerateGradientGM;) |