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