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