epoger@google.com | fd03db0 | 2011-07-28 14:24:55 +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 | */ |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 8 | #include "SampleCode.h" |
| 9 | #include "SkView.h" |
| 10 | #include "SkCanvas.h" |
| 11 | #include "SkShader.h" |
| 12 | #include "SkUtils.h" |
| 13 | #include "SkDevice.h" |
| 14 | |
| 15 | static void create_bitmap(SkBitmap* bitmap) { |
| 16 | const int W = 100; |
| 17 | const int H = 100; |
| 18 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, W, H); |
| 19 | bitmap->allocPixels(); |
| 20 | |
| 21 | SkCanvas canvas(*bitmap); |
| 22 | canvas.drawColor(SK_ColorRED); |
| 23 | SkPaint paint; |
| 24 | paint.setColor(SK_ColorBLUE); |
| 25 | canvas.drawCircle(SkIntToScalar(W)/2, SkIntToScalar(H)/2, SkIntToScalar(W)/2, paint); |
| 26 | } |
| 27 | |
| 28 | class DrawBitmapView : public SampleView { |
| 29 | SkPath fPath; |
| 30 | public: |
| 31 | DrawBitmapView() {} |
| 32 | |
| 33 | protected: |
| 34 | // overrides from SkEventSink |
| 35 | virtual bool onQuery(SkEvent* evt) { |
| 36 | if (SampleCode::TitleQ(*evt)) { |
| 37 | SampleCode::TitleR(evt, "DrawBitmap"); |
| 38 | return true; |
| 39 | } |
| 40 | return this->INHERITED::onQuery(evt); |
| 41 | } |
| 42 | |
| 43 | virtual void onDrawContent(SkCanvas* canvas) { |
| 44 | SkBitmap bitmap; |
| 45 | create_bitmap(&bitmap); |
| 46 | int x = bitmap.width() / 2; |
| 47 | int y = bitmap.height() / 2; |
| 48 | SkBitmap subset; |
| 49 | bitmap.extractSubset(&subset, SkIRect::MakeXYWH(x, y, x, y)); |
| 50 | |
| 51 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 52 | |
| 53 | canvas->drawBitmap(bitmap, 0, 0); |
| 54 | canvas->drawBitmap(subset, 0, 0); |
| 55 | |
| 56 | // Now do the same but with a device bitmap as source image |
| 57 | SkRefPtr<SkDevice> primaryDevice(canvas->getDevice()); |
| 58 | SkRefPtr<SkDevice> secondDevice(canvas->createCompatibleDevice( |
| 59 | SkBitmap::kARGB_8888_Config, bitmap.width(), |
| 60 | bitmap.height(), true)); |
| 61 | secondDevice->unref(); |
| 62 | SkCanvas secondCanvas(secondDevice.get()); |
| 63 | secondCanvas.writePixels(bitmap, 0, 0); |
| 64 | |
| 65 | SkBitmap deviceBitmap = secondDevice->accessBitmap(false); |
| 66 | SkBitmap deviceSubset; |
| 67 | deviceBitmap.extractSubset(&deviceSubset, |
| 68 | SkIRect::MakeXYWH(x, y, x, y)); |
| 69 | |
| 70 | canvas->translate(SkIntToScalar(120), SkIntToScalar(0)); |
| 71 | |
| 72 | canvas->drawBitmap(deviceBitmap, 0, 0); |
| 73 | canvas->drawBitmap(deviceSubset, 0, 0); |
| 74 | |
| 75 | } |
| 76 | |
| 77 | private: |
| 78 | typedef SampleView INHERITED; |
| 79 | }; |
| 80 | |
| 81 | ////////////////////////////////////////////////////////////////////////////// |
| 82 | |
| 83 | static SkView* MyFactory() { return new DrawBitmapView; } |
| 84 | static SkViewRegister reg(MyFactory); |