epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | */ |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 7 | #include "SampleCode.h" |
| 8 | #include "SkView.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkGradientShader.h" |
| 11 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 12 | static sk_sp<SkShader> setgrad(const SkRect& r, SkColor c0, SkColor c1) { |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 13 | SkColor colors[] = { c0, c1 }; |
| 14 | SkPoint pts[] = { { r.fLeft, r.fTop }, { r.fRight, r.fTop } }; |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 15 | return SkGradientShader::MakeLinear(pts, colors, nullptr, 2, SkShader::kClamp_TileMode); |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 16 | } |
| 17 | |
| 18 | static void test_alphagradients(SkCanvas* canvas) { |
| 19 | SkRect r; |
| 20 | r.set(SkIntToScalar(10), SkIntToScalar(10), |
| 21 | SkIntToScalar(410), SkIntToScalar(30)); |
| 22 | SkPaint p, p2; |
| 23 | p2.setStyle(SkPaint::kStroke_Style); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 24 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 25 | p.setShader(setgrad(r, 0xFF00FF00, 0x0000FF00)); |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 26 | canvas->drawRect(r, p); |
| 27 | canvas->drawRect(r, p2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 28 | |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 29 | r.offset(0, r.height() + SkIntToScalar(4)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 30 | p.setShader(setgrad(r, 0xFF00FF00, 0x00000000)); |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 31 | canvas->drawRect(r, p); |
| 32 | canvas->drawRect(r, p2); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 33 | |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 34 | r.offset(0, r.height() + SkIntToScalar(4)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 35 | p.setShader(setgrad(r, 0xFF00FF00, 0x00FF0000)); |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 36 | canvas->drawRect(r, p); |
| 37 | canvas->drawRect(r, p2); |
| 38 | } |
| 39 | |
| 40 | /////////////////////////////////////////////////////////////////////////////// |
| 41 | |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 42 | struct GradData { |
| 43 | int fCount; |
| 44 | const SkColor* fColors; |
| 45 | const SkScalar* fPos; |
| 46 | }; |
| 47 | |
| 48 | static const SkColor gColors[] = { |
| 49 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE, SK_ColorBLACK |
| 50 | }; |
| 51 | static const SkScalar gPos0[] = { 0, SK_Scalar1 }; |
| 52 | static const SkScalar gPos1[] = { SK_Scalar1/4, SK_Scalar1*3/4 }; |
| 53 | static const SkScalar gPos2[] = { |
| 54 | 0, SK_Scalar1/8, SK_Scalar1/2, SK_Scalar1*7/8, SK_Scalar1 |
| 55 | }; |
| 56 | |
| 57 | static const GradData gGradData[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 58 | { 2, gColors, nullptr }, |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 59 | { 2, gColors, gPos0 }, |
| 60 | { 2, gColors, gPos1 }, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 61 | { 5, gColors, nullptr }, |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 62 | { 5, gColors, gPos2 } |
| 63 | }; |
| 64 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 65 | static sk_sp<SkShader> MakeLinear(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { |
| 66 | return SkGradientShader::MakeLinear(pts, data.fColors, data.fPos, data.fCount, tm); |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 67 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 68 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 69 | static sk_sp<SkShader> MakeRadial(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 70 | SkPoint center; |
| 71 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 72 | SkScalarAve(pts[0].fY, pts[1].fY)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 73 | return SkGradientShader::MakeRadial(center, center.fX, data.fColors, |
| 74 | data.fPos, data.fCount, tm); |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 75 | } |
| 76 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 77 | static sk_sp<SkShader> MakeSweep(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 78 | SkPoint center; |
| 79 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 80 | SkScalarAve(pts[0].fY, pts[1].fY)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 81 | return SkGradientShader::MakeSweep(center.fX, center.fY, data.fColors, data.fPos, data.fCount); |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 82 | } |
| 83 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 84 | static sk_sp<SkShader> Make2Conical(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm) { |
reed@android.com | 8e2af7e | 2009-09-22 20:14:42 +0000 | [diff] [blame] | 85 | SkPoint center0, center1; |
| 86 | center0.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 87 | SkScalarAve(pts[0].fY, pts[1].fY)); |
| 88 | center1.set(SkScalarInterp(pts[0].fX, pts[1].fX, SkIntToScalar(3)/5), |
| 89 | SkScalarInterp(pts[0].fY, pts[1].fY, SkIntToScalar(1)/4)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 90 | return SkGradientShader::MakeTwoPointConical( |
reed@android.com | 8e2af7e | 2009-09-22 20:14:42 +0000 | [diff] [blame] | 91 | center1, (pts[1].fX - pts[0].fX) / 7, |
| 92 | center0, (pts[1].fX - pts[0].fX) / 2, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 93 | data.fColors, data.fPos, data.fCount, tm); |
reed@android.com | 8e2af7e | 2009-09-22 20:14:42 +0000 | [diff] [blame] | 94 | } |
| 95 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 96 | static sk_sp<SkShader> Make2ConicalConcentric(const SkPoint pts[2], const GradData& data, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 97 | SkShader::TileMode tm) { |
bsalomon@google.com | dc66c42 | 2011-04-06 19:35:37 +0000 | [diff] [blame] | 98 | SkPoint center; |
| 99 | center.set(SkScalarAve(pts[0].fX, pts[1].fX), |
| 100 | SkScalarAve(pts[0].fY, pts[1].fY)); |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 101 | return SkGradientShader::MakeTwoPointConical( |
bsalomon@google.com | dc66c42 | 2011-04-06 19:35:37 +0000 | [diff] [blame] | 102 | center, (pts[1].fX - pts[0].fX) / 7, |
| 103 | center, (pts[1].fX - pts[0].fX) / 2, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 104 | data.fColors, data.fPos, data.fCount, tm); |
bsalomon@google.com | dc66c42 | 2011-04-06 19:35:37 +0000 | [diff] [blame] | 105 | } |
| 106 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 107 | typedef sk_sp<SkShader> (*GradMaker)(const SkPoint pts[2], const GradData& data, SkShader::TileMode tm); |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame] | 108 | |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 109 | static const GradMaker gGradMakers[] = { |
reed | 71a6cbf | 2015-05-04 08:32:51 -0700 | [diff] [blame] | 110 | MakeLinear, MakeRadial, MakeSweep, Make2Conical, Make2ConicalConcentric |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | /////////////////////////////////////////////////////////////////////////////// |
| 114 | |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 115 | class GradientsView : public SampleView { |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 116 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 117 | GradientsView() { |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 118 | this->setBGColor(0xFFDDDDDD); |
| 119 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 120 | |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 121 | protected: |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 122 | bool onQuery(SkEvent* evt) override { |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 123 | if (SampleCode::TitleQ(*evt)) { |
| 124 | SampleCode::TitleR(evt, "Gradients"); |
| 125 | return true; |
| 126 | } |
| 127 | return this->INHERITED::onQuery(evt); |
| 128 | } |
| 129 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 130 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 131 | SkPoint pts[2] = { |
| 132 | { 0, 0 }, |
| 133 | { SkIntToScalar(100), SkIntToScalar(100) } |
| 134 | }; |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 135 | SkRect r = { 0, 0, SkIntToScalar(100), SkIntToScalar(100) }; |
| 136 | SkPaint paint; |
reed@android.com | 32a4249 | 2009-07-10 03:33:52 +0000 | [diff] [blame] | 137 | paint.setDither(true); |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 138 | |
| 139 | canvas->save(); |
reed@android.com | 8e2af7e | 2009-09-22 20:14:42 +0000 | [diff] [blame] | 140 | canvas->translate(SkIntToScalar(20), SkIntToScalar(10)); |
bsalomon@google.com | dc66c42 | 2011-04-06 19:35:37 +0000 | [diff] [blame] | 141 | |
| 142 | for (int tm = 0; tm < SkShader::kTileModeCount; ++tm) { |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 143 | canvas->save(); |
bsalomon@google.com | dc66c42 | 2011-04-06 19:35:37 +0000 | [diff] [blame] | 144 | for (size_t i = 0; i < SK_ARRAY_COUNT(gGradData); i++) { |
| 145 | canvas->save(); |
| 146 | for (size_t j = 0; j < SK_ARRAY_COUNT(gGradMakers); j++) { |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 147 | paint.setShader(gGradMakers[j](pts, gGradData[i], (SkShader::TileMode)tm)); |
bsalomon@google.com | dc66c42 | 2011-04-06 19:35:37 +0000 | [diff] [blame] | 148 | canvas->drawRect(r, paint); |
| 149 | canvas->translate(0, SkIntToScalar(120)); |
| 150 | } |
| 151 | canvas->restore(); |
| 152 | canvas->translate(SkIntToScalar(120), 0); |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 153 | } |
| 154 | canvas->restore(); |
bsalomon@google.com | dc66c42 | 2011-04-06 19:35:37 +0000 | [diff] [blame] | 155 | canvas->translate(SK_ARRAY_COUNT(gGradData)*SkIntToScalar(120), 0); |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 156 | } |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 157 | canvas->restore(); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 158 | |
reed@android.com | 1c12abe | 2009-07-02 15:01:02 +0000 | [diff] [blame] | 159 | canvas->translate(0, SkIntToScalar(370)); |
caryclark@google.com | 02939ce | 2012-06-06 12:09:51 +0000 | [diff] [blame] | 160 | if (false) { // avoid bit rot, suppress warning |
| 161 | test_alphagradients(canvas); |
| 162 | } |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 163 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 164 | |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 165 | private: |
reed@google.com | f218339 | 2011-04-22 14:10:48 +0000 | [diff] [blame] | 166 | typedef SampleView INHERITED; |
reed@android.com | a3c0588 | 2009-04-02 20:49:39 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | /////////////////////////////////////////////////////////////////////////////// |
| 170 | |
| 171 | static SkView* MyFactory() { return new GradientsView; } |
| 172 | static SkViewRegister reg(MyFactory); |