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[] = { |
| 33 | { 40, gColors, NULL }, |
| 34 | // { 2, gColors, gPos }, |
| 35 | // { 2, gCol2, NULL }, |
| 36 | }; |
| 37 | |
| 38 | static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, |
| 39 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 40 | return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, |
| 41 | data.fCount, tm, mapper); |
| 42 | } |
| 43 | |
| 44 | static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, |
| 45 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 46 | SkPoint center; |
| 47 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 48 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 49 | return SkGradientShader::CreateRadial(center, center.fX, data.fColors, |
| 50 | data.fPos, data.fCount, tm, mapper); |
| 51 | } |
| 52 | |
| 53 | static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, |
| 54 | SkShader::TileMode, SkUnitMapper* mapper) { |
| 55 | SkPoint center; |
| 56 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 57 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 58 | return SkGradientShader::CreateSweep(center.fX, center.fY, data.fColors, |
| 59 | data.fPos, data.fCount, mapper); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, |
| 64 | SkShader::TileMode tm, SkUnitMapper* mapper); |
| 65 | static const GradMaker gGradMakers[] = { |
skia.committer@gmail.com | 9e1ec1a | 2013-07-10 07:00:58 +0000 | [diff] [blame] | 66 | MakeLinear, MakeRadial, MakeSweep, |
commit-bot@chromium.org | b686539 | 2013-07-09 19:43:29 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | /////////////////////////////////////////////////////////////////////////////// |
| 70 | |
| 71 | class GradientsGM : public GM { |
| 72 | public: |
| 73 | GradientsGM() { |
| 74 | this->setBGColor(0xFFDDDDDD); |
| 75 | } |
| 76 | |
| 77 | protected: |
| 78 | SkString onShortName() SK_OVERRIDE { return SkString("gradient_dirty_laundry"); } |
| 79 | virtual SkISize onISize() SK_OVERRIDE { return make_isize(640, 615); } |
| 80 | |
| 81 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 82 | SkPoint pts[2] = { { 0, 0 }, |
| 83 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 84 | }; |
| 85 | SkShader::TileMode tm = SkShader::kClamp_TileMode; |
| 86 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
| 87 | SkPaint paint; |
| 88 | paint.setAntiAlias(true); |
| 89 | |
| 90 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 91 | for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
| 92 | canvas->save(); |
| 93 | for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
| 94 | SkShader* shader = gGradMakers[j](pts, gGradData[i], tm, NULL); |
| 95 | paint.setShader(shader)->unref(); |
| 96 | canvas->drawRect(r, paint); |
| 97 | canvas->translate(0, SkIntToScalar(120)); |
| 98 | } |
| 99 | canvas->restore(); |
| 100 | canvas->translate(SkIntToScalar(120), 0); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | private: |
| 105 | typedef GM INHERITED; |
| 106 | }; |
| 107 | |
| 108 | /////////////////////////////////////////////////////////////////////////////// |
| 109 | |
| 110 | static GM* MyFactory(void*) { return new GradientsGM; } |
| 111 | static GMRegistry reg(MyFactory); |