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@google.com | ceff287 | 2011-07-27 18:21:37 +0000 | [diff] [blame] | 7 | #include "SampleCode.h" |
| 8 | #include "SkView.h" |
Mike Reed | 75ae421 | 2018-01-23 11:24:08 -0500 | [diff] [blame^] | 9 | #include "SkBitmap.h" |
reed@google.com | ceff287 | 2011-07-27 18:21:37 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkCornerPathEffect.h" |
reed@google.com | ceff287 | 2011-07-27 18:21:37 +0000 | [diff] [blame] | 12 | #include "SkGradientShader.h" |
| 13 | #include "SkPath.h" |
| 14 | #include "SkRegion.h" |
| 15 | #include "SkShader.h" |
| 16 | #include "SkUtils.h" |
| 17 | |
| 18 | static void create_bitmap(SkBitmap* bitmap) { |
| 19 | const int W = 100; |
| 20 | const int H = 100; |
commit-bot@chromium.org | a8c1831 | 2014-02-17 02:55:57 +0000 | [diff] [blame] | 21 | bitmap->allocN32Pixels(W, H); |
reed@google.com | ceff287 | 2011-07-27 18:21:37 +0000 | [diff] [blame] | 22 | |
| 23 | SkCanvas canvas(*bitmap); |
| 24 | canvas.drawColor(SK_ColorRED); |
| 25 | SkPaint paint; |
| 26 | paint.setColor(SK_ColorBLUE); |
| 27 | canvas.drawCircle(SkIntToScalar(W)/2, SkIntToScalar(H)/2, SkIntToScalar(W)/2, paint); |
| 28 | } |
| 29 | |
| 30 | class WritePixelsView : public SampleView { |
| 31 | SkPath fPath; |
| 32 | public: |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 33 | WritePixelsView() {} |
| 34 | |
reed@google.com | ceff287 | 2011-07-27 18:21:37 +0000 | [diff] [blame] | 35 | protected: |
| 36 | // overrides from SkEventSink |
| 37 | virtual bool onQuery(SkEvent* evt) { |
| 38 | if (SampleCode::TitleQ(*evt)) { |
| 39 | SampleCode::TitleR(evt, "WritePixels"); |
| 40 | return true; |
| 41 | } |
| 42 | return this->INHERITED::onQuery(evt); |
| 43 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 44 | |
reed@google.com | ceff287 | 2011-07-27 18:21:37 +0000 | [diff] [blame] | 45 | virtual void onDrawContent(SkCanvas* canvas) { |
| 46 | SkBitmap bitmap; |
| 47 | create_bitmap(&bitmap); |
| 48 | int x = bitmap.width() / 2; |
| 49 | int y = bitmap.height() / 2; |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 50 | |
reed@google.com | ceff287 | 2011-07-27 18:21:37 +0000 | [diff] [blame] | 51 | SkBitmap subset; |
| 52 | bitmap.extractSubset(&subset, SkIRect::MakeXYWH(x, y, x, y)); |
| 53 | |
| 54 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 55 | |
| 56 | canvas->writePixels(bitmap, 0, 0); |
| 57 | canvas->writePixels(subset, 0, 0); |
| 58 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 59 | |
reed@google.com | ceff287 | 2011-07-27 18:21:37 +0000 | [diff] [blame] | 60 | private: |
| 61 | typedef SampleView INHERITED; |
| 62 | }; |
| 63 | |
| 64 | ////////////////////////////////////////////////////////////////////////////// |
| 65 | |
| 66 | static SkView* MyFactory() { return new WritePixelsView; } |
| 67 | static SkViewRegister reg(MyFactory); |