commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +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 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 8 | #include "sk_tool_utils.h" |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 9 | #include "SkGradientShader.h" |
| 10 | |
| 11 | using namespace skiagm; |
| 12 | |
| 13 | struct GradData { |
| 14 | int fCount; |
| 15 | const SkColor* fColors; |
| 16 | const SkScalar* fPos; |
| 17 | }; |
| 18 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 19 | constexpr SkColor gColors[] = { |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 20 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK, |
| 21 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK, |
| 22 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK, |
| 23 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK, |
| 24 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK, |
| 25 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK, |
| 26 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK, |
| 27 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK, |
| 28 | }; |
| 29 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 30 | //constexpr SkScalar gPos[] = { SK_Scalar1*999/2000, SK_Scalar1*1001/2000 }; |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 31 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 32 | constexpr GradData gGradData[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 33 | { 40, gColors, nullptr }, |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 34 | // { 2, gColors, gPos }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 35 | // { 2, gCol2, nullptr }, |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 38 | static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { |
| 39 | return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm); |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 40 | } |
| 41 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 42 | static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { |
| 43 | const SkPoint pt{ SkScalarAve(pts[0].fX, pts[1].fX), SkScalarAve(pts[0].fY, pts[1].fY) }; |
| 44 | return SkGradientShader::MakeRadial(pt, pt.fX, data.fColors, data.fPos, data.fCount, tm); |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 45 | } |
| 46 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 47 | static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode) { |
| 48 | const SkPoint pt{ SkScalarAve(pts[0].fX, pts[1].fX), SkScalarAve(pts[0].fY, pts[1].fY) }; |
| 49 | return SkGradientShader::MakeSweep(pt.fX, pt.fY, data.fColors, data.fPos, data.fCount); |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 53 | typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData&, SkShader::TileMode); |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 54 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 55 | constexpr GradMaker gGradMakers[] = { |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 56 | MakeLinear, MakeRadial, MakeSweep, |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | /////////////////////////////////////////////////////////////////////////////// |
| 60 | |
| 61 | class GradientsGM : public GM { |
| 62 | public: |
| 63 | GradientsGM() { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 64 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 68 | SkString onShortName() override { return SkString("gradient_dirty_laundry"); } |
| 69 | SkISize onISize() override { return SkISize::Make(640, 615); } |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 70 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 71 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 72 | SkPoint pts[2] = { { 0, 0 }, |
| 73 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 74 | }; |
| 75 | SkShader::TileMode tm = SkShader::kClamp_TileMode; |
| 76 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
| 77 | SkPaint paint; |
| 78 | paint.setAntiAlias(true); |
| 79 | |
| 80 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 81 | for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
| 82 | canvas->save(); |
| 83 | for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 84 | paint.setShader(gGradMakers[j](pts, gGradData[i], tm)); |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 85 | canvas->drawRect(r, paint); |
| 86 | canvas->translate(0, SkIntToScalar(120)); |
| 87 | } |
| 88 | canvas->restore(); |
| 89 | canvas->translate(SkIntToScalar(120), 0); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | private: |
| 94 | typedef GM INHERITED; |
| 95 | }; |
| 96 | |
| 97 | /////////////////////////////////////////////////////////////////////////////// |
| 98 | |
| 99 | static GM* MyFactory(void*) { return new GradientsGM; } |
| 100 | static GMRegistry reg(MyFactory); |