bsalomon@google.com | 82d1223 | 2013-09-09 15:36:26 +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" |
| 8 | #include "SkGradientShader.h" |
| 9 | |
| 10 | using namespace skiagm; |
| 11 | |
| 12 | struct GradData { |
| 13 | int fCount; |
| 14 | const SkColor* fColors; |
| 15 | const SkScalar* fPos; |
| 16 | }; |
| 17 | |
| 18 | static const SkColor gColors[] = { |
| 19 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, |
| 20 | }; |
| 21 | |
| 22 | static const GradData gGradData[] = { |
| 23 | { 1, gColors, NULL }, |
| 24 | { 2, gColors, NULL }, |
| 25 | { 3, gColors, NULL }, |
| 26 | { 4, gColors, NULL }, |
| 27 | }; |
| 28 | |
| 29 | static SkShader* MakeLinear(const SkPoint pts[2], const GradData& data, |
| 30 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 31 | return SkGradientShader::CreateLinear(pts, data.fColors, data.fPos, |
| 32 | data.fCount, tm, mapper); |
| 33 | } |
| 34 | |
| 35 | static SkShader* MakeRadial(const SkPoint pts[2], const GradData& data, |
| 36 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 37 | SkPoint center; |
| 38 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 39 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 40 | return SkGradientShader::CreateRadial(center, center.fX, data.fColors, |
| 41 | data.fPos, data.fCount, tm, mapper); |
| 42 | } |
| 43 | |
| 44 | static SkShader* MakeSweep(const SkPoint pts[2], const GradData& data, |
| 45 | SkShader::TileMode, 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::CreateSweep(center.fX, center.fY, data.fColors, |
| 50 | data.fPos, data.fCount, mapper); |
| 51 | } |
| 52 | |
| 53 | static SkShader* Make2Radial(const SkPoint pts[2], const GradData& data, |
| 54 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 55 | SkPoint center0, center1; |
| 56 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 57 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 58 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 59 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
| 60 | return SkGradientShader::CreateTwoPointRadial( |
| 61 | center1, (pts[1].fX - pts[0].fX) / 7, |
| 62 | center0, (pts[1].fX - pts[0].fX) / 2, |
| 63 | data.fColors, data.fPos, data.fCount, tm, mapper); |
| 64 | } |
| 65 | |
| 66 | static SkShader* Make2Conical(const SkPoint pts[2], const GradData& data, |
| 67 | SkShader::TileMode tm, SkUnitMapper* mapper) { |
| 68 | SkPoint center0, center1; |
| 69 | SkScalar radius0 = SkScalarDiv(pts[1].fX - pts[0].fX, 10); |
| 70 | SkScalar radius1 = SkScalarDiv(pts[1].fX - pts[0].fX, 3); |
| 71 | center0.set(pts[0].fX + radius0, pts[0].fY + radius0); |
| 72 | center1.set(pts[1].fX - radius1, pts[1].fY - radius1); |
| 73 | return SkGradientShader::CreateTwoPointConical(center1, radius1, |
| 74 | center0, radius0, |
| 75 | data.fColors, data.fPos, |
| 76 | data.fCount, tm, mapper); |
| 77 | } |
| 78 | |
| 79 | |
| 80 | typedef SkShader* (*GradMaker)(const SkPoint pts[2], const GradData& data, |
| 81 | SkShader::TileMode tm, SkUnitMapper* mapper); |
| 82 | static const GradMaker gGradMakers[] = { |
| 83 | MakeLinear, MakeRadial, MakeSweep, Make2Radial, Make2Conical, |
| 84 | }; |
| 85 | |
| 86 | /////////////////////////////////////////////////////////////////////////////// |
| 87 | |
| 88 | class GradientsNoTextureGM : public GM { |
| 89 | public: |
| 90 | GradientsNoTextureGM() { |
| 91 | this->setBGColor(0xFFDDDDDD); |
| 92 | } |
| 93 | |
| 94 | protected: |
| 95 | SkString onShortName() SK_OVERRIDE { return SkString("gradients_no_texture"); } |
| 96 | virtual SkISize onISize() SK_OVERRIDE { return make_isize(640, 615); } |
| 97 | |
| 98 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
| 99 | static const SkPoint kPts[2] = { { 0, 0 }, |
| 100 | { SkIntToScalar(50), SkIntToScalar(50) } }; |
| 101 | static const SkShader::TileMode kTM = SkShader::kClamp_TileMode; |
| 102 | SkRect kRect = { 0, 0, SkIntToScalar(50), SkIntToScalar(50) }; |
| 103 | SkPaint paint; |
| 104 | paint.setAntiAlias(true); |
| 105 | |
| 106 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 107 | static const uint8_t kAlphas[] = { 0xff, 0x40 }; |
| 108 | for (size_t a = 0; a < SK_ARRAY_COUNT(kAlphas); ++a) { |
| 109 | for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); ++i) { |
| 110 | canvas->save(); |
| 111 | for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); ++j) { |
| 112 | SkShader* shader = gGradMakers[j](kPts, gGradData[i], kTM, NULL); |
| 113 | paint.setShader(shader)->unref(); |
| 114 | paint.setAlpha(kAlphas[a]); |
| 115 | canvas->drawRect(kRect, paint); |
| 116 | canvas->translate(0, SkIntToScalar(kRect.height() + 20)); |
| 117 | } |
| 118 | canvas->restore(); |
| 119 | canvas->translate(SkIntToScalar(kRect.width() + 20), 0); |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | private: |
| 125 | typedef GM INHERITED; |
| 126 | }; |
| 127 | |
| 128 | /////////////////////////////////////////////////////////////////////////////// |
| 129 | |
| 130 | DEF_GM( return SkNEW(GradientsNoTextureGM)); |