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