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() } }; |
| 15 | return SkGradientShader::CreateLinear(pts, colors, NULL, count, |
| 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 }; |
| 21 | return SkGradientShader::CreateRadial(center, size.width()/2, colors, NULL, count, |
| 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, |
| 29 | colors, NULL, count, |
| 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, |
| 35 | colors, NULL, count); |
| 36 | } |
| 37 | |
| 38 | class ShallowGradientGM : public skiagm::GM { |
| 39 | public: |
| 40 | ShallowGradientGM(MakeShaderProc proc, const char name[]) : fProc(proc) { |
| 41 | fName.printf("shallow_gradient_%s", name); |
| 42 | } |
| 43 | |
| 44 | protected: |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 45 | uint32_t onGetFlags() const SK_OVERRIDE { |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 46 | if (fName.contains("linear") || fName.contains("radial")) { |
| 47 | return kSkipTiled_Flag; |
| 48 | } |
| 49 | return 0; |
| 50 | } |
| 51 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 52 | SkString onShortName() SK_OVERRIDE { |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 53 | return fName; |
| 54 | } |
| 55 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 56 | SkISize onISize() SK_OVERRIDE { |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 57 | return SkISize::Make(800, 800); |
| 58 | } |
| 59 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 60 | void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 61 | const SkColor colors[] = { 0xFF555555, 0xFF444444 }; |
| 62 | const int colorCount = SK_ARRAY_COUNT(colors); |
| 63 | |
| 64 | SkRect r = { 0, 0, this->width(), this->height() }; |
| 65 | SkSize size = SkSize::Make(r.width(), r.height()); |
| 66 | |
| 67 | SkPaint paint; |
robertphillips@google.com | b62ce70 | 2013-03-27 21:31:21 +0000 | [diff] [blame] | 68 | paint.setShader(fProc(colors, colorCount, size))->unref(); |
reed@google.com | 83f7c65 | 2013-02-04 16:56:15 +0000 | [diff] [blame] | 69 | canvas->drawRect(r, paint); |
| 70 | } |
| 71 | |
| 72 | private: |
| 73 | MakeShaderProc fProc; |
| 74 | SkString fName; |
| 75 | |
| 76 | typedef skiagm::GM INHERITED; |
| 77 | }; |
| 78 | |
| 79 | /////////////////////////////////////////////////////////////////////////////// |
| 80 | |
| 81 | DEF_GM( return new ShallowGradientGM(shader_linear, "linear"); ) |
| 82 | DEF_GM( return new ShallowGradientGM(shader_radial, "radial"); ) |
| 83 | DEF_GM( return new ShallowGradientGM(shader_conical, "conical"); ) |
| 84 | DEF_GM( return new ShallowGradientGM(shader_sweep, "sweep"); ) |