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 { |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 53 | const SkColor colors[] = { sk_tool_utils::color_to_565(0xFF555555), |
caryclark | 1259601 | 2015-07-29 05:27:47 -0700 | [diff] [blame] | 54 | sk_tool_utils::color_to_565(0xFF444444) }; |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 55 | const int colorCount = SK_ARRAY_COUNT(colors); |
| 56 | |
| 57 | SkRect r = { 0, 0, this->width(), this->height() }; |
| 58 | SkSize size = SkSize::Make(r.width(), r.height()); |
| 59 | |
| 60 | SkPaint paint; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 61 | paint.setShader(fProc(colors, colorCount, size)); |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 62 | paint.setDither(fDither); |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 63 | canvas->drawRect(r, paint); |
| 64 | } |
| 65 | |
| 66 | private: |
| 67 | MakeShaderProc fProc; |
| 68 | SkString fName; |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 69 | bool fDither; |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 70 | |
| 71 | typedef skiagm::GM INHERITED; |
| 72 | }; |
| 73 | |
| 74 | /////////////////////////////////////////////////////////////////////////////// |
| 75 | |
fmalita | 063675b | 2015-10-12 10:41:48 -0700 | [diff] [blame] | 76 | DEF_GM( return new ShallowGradientGM(shader_linear, "linear", true); ) |
| 77 | DEF_GM( return new ShallowGradientGM(shader_radial, "radial", true); ) |
| 78 | DEF_GM( return new ShallowGradientGM(shader_conical, "conical", true); ) |
| 79 | DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep", true); ) |
| 80 | |
| 81 | DEF_GM( return new ShallowGradientGM(shader_linear, "linear_nodither", false); ) |
| 82 | DEF_GM( return new ShallowGradientGM(shader_radial, "radial_nodither", false); ) |
| 83 | DEF_GM( return new ShallowGradientGM(shader_conical, "conical_nodither", false); ) |
| 84 | DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep_nodither", false); ) |