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