reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 8 | #include "gm.h" |
| 9 | #include "SkGradientShader.h" |
| 10 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 11 | typedef sk_sp<SkShader> (*MakeShaderProc)(const SkColor[], int count, const SkSize&); |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 12 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 13 | static sk_sp<SkShader> shader_linear(const SkColor colors[], int count, const SkSize& size) { |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 14 | SkPoint pts[] = { { 0, 0 }, { size.width(), size.height() } }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 15 | return SkGradientShader::MakeLinear(pts, colors, nullptr, count, SkShader::kClamp_TileMode); |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 16 | } |
| 17 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 18 | static sk_sp<SkShader> shader_radial(const SkColor colors[], int count, const SkSize& size) { |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 19 | SkPoint center = { size.width()/2, size.height()/2 }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 20 | return SkGradientShader::MakeRadial(center, size.width()/2, colors, nullptr, count, |
| 21 | SkShader::kClamp_TileMode); |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 22 | } |
| 23 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 24 | static sk_sp<SkShader> shader_conical(const SkColor colors[], int count, const SkSize& size) { |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 25 | SkPoint center = { size.width()/2, size.height()/2 }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 26 | return SkGradientShader::MakeTwoPointConical(center, size.width()/64, center, size.width()/2, |
| 27 | colors, nullptr, count, SkShader::kClamp_TileMode); |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 28 | } |
| 29 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 30 | static sk_sp<SkShader> shader_sweep(const SkColor colors[], int count, const SkSize& size) { |
| 31 | return SkGradientShader::MakeSweep(size.width()/2, size.height()/2, colors, nullptr, count); |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | class ShallowGradientGM : public skiagm::GM { |
| 35 | public: |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 36 | ShallowGradientGM(MakeShaderProc proc, const char name[], bool dither) |
| 37 | : fProc(proc) |
| 38 | , fDither(dither) { |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 39 | fName.printf("shallow_gradient_%s", name); |
| 40 | } |
| 41 | |
| 42 | protected: |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 43 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 44 | SkString onShortName() override { |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 45 | return fName; |
| 46 | } |
| 47 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 48 | SkISize onISize() override { |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 49 | return SkISize::Make(800, 800); |
| 50 | } |
| 51 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 52 | void onDraw(SkCanvas* canvas) override { |
Mike Klein | cbd83bb | 2018-08-16 09:32:46 -0400 | [diff] [blame] | 53 | const SkColor colors[] = { 0xFF555555, 0xFF444444 }; |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 54 | const int colorCount = SK_ARRAY_COUNT(colors); |
| 55 | |
| 56 | SkRect r = { 0, 0, this->width(), this->height() }; |
| 57 | SkSize size = SkSize::Make(r.width(), r.height()); |
| 58 | |
| 59 | SkPaint paint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 60 | paint.setShader(fProc(colors, colorCount, size)); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 61 | paint.setDither(fDither); |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 62 | canvas->drawRect(r, paint); |
| 63 | } |
| 64 | |
| 65 | private: |
| 66 | MakeShaderProc fProc; |
| 67 | SkString fName; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 68 | bool fDither; |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 69 | |
| 70 | typedef skiagm::GM INHERITED; |
| 71 | }; |
| 72 | |
| 73 | /////////////////////////////////////////////////////////////////////////////// |
| 74 | |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 75 | DEF_GM( return new ShallowGradientGM(shader_linear, "linear", true); ) |
| 76 | DEF_GM( return new ShallowGradientGM(shader_radial, "radial", true); ) |
| 77 | DEF_GM( return new ShallowGradientGM(shader_conical, "conical", true); ) |
| 78 | DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep", true); ) |
| 79 | |
| 80 | DEF_GM( return new ShallowGradientGM(shader_linear, "linear_nodither", false); ) |
| 81 | DEF_GM( return new ShallowGradientGM(shader_radial, "radial_nodither", false); ) |
| 82 | DEF_GM( return new ShallowGradientGM(shader_conical, "conical_nodither", false); ) |
| 83 | DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep_nodither", false); ) |