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 | */ |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 7 | #include "Sample.h" |
Mike Reed | 75ae421 | 2018-01-23 11:24:08 -0500 | [diff] [blame] | 8 | #include "SkBitmap.h" |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 9 | #include "SkCanvas.h" |
| 10 | #include "SkShader.h" |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 11 | |
| 12 | static void make_bitmap(SkBitmap* bm) { |
| 13 | const int W = 100; |
| 14 | const int H = 100; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 15 | bm->allocN32Pixels(W, H); |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 16 | |
| 17 | SkPaint paint; |
| 18 | SkCanvas canvas(*bm); |
| 19 | canvas.drawColor(SK_ColorWHITE); |
| 20 | |
| 21 | const SkColor colors[] = { |
| 22 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorWHITE |
| 23 | }; |
| 24 | |
| 25 | for (int ix = 0; ix < W; ix += 1) { |
| 26 | SkScalar x = SkIntToScalar(ix) + SK_ScalarHalf; |
| 27 | paint.setColor(colors[ix & 3]); |
| 28 | canvas.drawLine(x, 0, x, SkIntToScalar(H - 1), paint); |
| 29 | } |
| 30 | paint.setColor(SK_ColorGRAY); |
| 31 | canvas.drawLine(0, 0, SkIntToScalar(W), 0, paint); |
| 32 | } |
| 33 | |
| 34 | static void make_paint(SkPaint* paint, SkShader::TileMode tm) { |
| 35 | SkBitmap bm; |
| 36 | make_bitmap(&bm); |
| 37 | |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 38 | paint->setShader(SkShader::MakeBitmapShader(bm, tm, tm)); |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 39 | } |
| 40 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 41 | class RepeatTileView : public Sample { |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 42 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 43 | RepeatTileView() { |
reed@google.com | 81e3d7f | 2011-06-01 12:42:36 +0000 | [diff] [blame] | 44 | this->setBGColor(SK_ColorGRAY); |
| 45 | } |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 46 | |
| 47 | protected: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 48 | bool onQuery(Sample::Event* evt) override { |
| 49 | if (Sample::TitleQ(*evt)) { |
| 50 | Sample::TitleR(evt, "RepeatTile"); |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 51 | return true; |
| 52 | } |
| 53 | return this->INHERITED::onQuery(evt); |
| 54 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 55 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 56 | void onDrawContent(SkCanvas* canvas) override { |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 57 | SkPaint paint; |
| 58 | make_paint(&paint, SkShader::kRepeat_TileMode); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 59 | |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 60 | // canvas->scale(SK_Scalar1*2, SK_Scalar1); |
| 61 | canvas->translate(SkIntToScalar(100), SkIntToScalar(100)); |
| 62 | canvas->drawPaint(paint); |
| 63 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 64 | |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 65 | private: |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 66 | typedef Sample INHERITED; |
reed@android.com | f7d5726 | 2009-08-13 19:33:44 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | ////////////////////////////////////////////////////////////////////////////// |
| 70 | |
Ben Wagner | b2c4ea6 | 2018-08-08 11:36:17 -0400 | [diff] [blame] | 71 | DEF_SAMPLE( return new RepeatTileView(); ) |