commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2013 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | #include "gm.h" |
| 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 | |
| 19 | static const SkColor gColors[] = { |
| 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 | |
| 30 | //static const SkScalar gPos[] = { SK_Scalar1*999/2000, SK_Scalar1*1001/2000 }; |
| 31 | |
| 32 | static const 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 | |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 38 | static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { |
| 39 | return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, data.fCount, tm); |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 40 | } |
| 41 | |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 42 | static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 43 | SkPoint center; |
| 44 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 45 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 46 | return SkGradientShader::CreateRadial(center, center.fX, data.fColors, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 47 | data.fPos, data.fCount, tm); |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 48 | } |
| 49 | |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 50 | static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode) { |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 51 | SkPoint center; |
| 52 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 53 | SkScalarAve(pts[0].fY, pts[1].fY)); |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 54 | return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount); |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 58 | typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm); |
| 59 | |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 60 | static const GradMaker gGradMakers[] = { |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 61 | MakeLinear, MakeRadial, MakeSweep, |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | /////////////////////////////////////////////////////////////////////////////// |
| 65 | |
| 66 | class GradientsGM : public GM { |
| 67 | public: |
| 68 | GradientsGM() { |
caryclark | 65cdba6 | 2015-06-15 06:51:08 -0700 | [diff] [blame] | 69 | this->setBGColor(sk_tool_utils::color_to_565(0xFFDDDDDD)); |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 73 | SkString onShortName() override { return SkString("gradient_dirty_laundry"); } |
| 74 | SkISize onISize() override { return SkISize::Make(640, 615); } |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 75 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 76 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 77 | SkPoint pts[2] = { { 0, 0 }, |
| 78 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 79 | }; |
| 80 | SkShader::TileMode tm = SkShader::kClamp_TileMode; |
| 81 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
| 82 | SkPaint paint; |
| 83 | paint.setAntiAlias(true); |
| 84 | |
| 85 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 86 | for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
| 87 | canvas->save(); |
| 88 | for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 89 | SkShader* shader = gGradMakers[j](pts, gGradData[i], tm); |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 90 | paint.setShader(shader)->unref(); |
| 91 | canvas->drawRect(r, paint); |
| 92 | canvas->translate(0, SkIntToScalar(120)); |
| 93 | } |
| 94 | canvas->restore(); |
| 95 | canvas->translate(SkIntToScalar(120), 0); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | private: |
| 100 | typedef GM INHERITED; |
| 101 | }; |
| 102 | |
| 103 | /////////////////////////////////////////////////////////////////////////////// |
| 104 | |
| 105 | static GM* MyFactory(void*) { return new GradientsGM; } |
| 106 | static GMRegistry reg(MyFactory); |