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 | */ |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 8 | #include "gm.h" |
| 9 | #include "SkBitmap.h" |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 11 | #include "SkDevice.h" |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 12 | #include "SkString.h" |
| 13 | |
| 14 | namespace skiagm { |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 15 | |
| 16 | static void create_bitmap(SkBitmap* bitmap) { |
| 17 | const int W = 100; |
| 18 | const int H = 100; |
| 19 | bitmap->setConfig(SkBitmap::kARGB_8888_Config, W, H); |
| 20 | bitmap->allocPixels(); |
| 21 | |
| 22 | SkCanvas canvas(*bitmap); |
| 23 | canvas.drawColor(SK_ColorRED); |
| 24 | SkPaint paint; |
| 25 | paint.setColor(SK_ColorBLUE); |
| 26 | canvas.drawCircle(SkIntToScalar(W)/2, SkIntToScalar(H)/2, SkIntToScalar(W)/2, paint); |
| 27 | } |
| 28 | |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 29 | class ExtractBitmapGM : public GM { |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 30 | public: |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 31 | ExtractBitmapGM() {} |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 32 | |
| 33 | protected: |
| 34 | // overrides from SkEventSink |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 35 | virtual SkString onShortName() SK_OVERRIDE { |
| 36 | return SkString("extractbitmap"); |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 37 | } |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 38 | |
| 39 | virtual SkISize onISize() SK_OVERRIDE { |
| 40 | return make_isize(600, 600); |
| 41 | } |
| 42 | |
| 43 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 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); |
scroggo@google.com | aae9e27 | 2012-07-02 14:36:33 +0000 | [diff] [blame] | 55 | /* |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 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); |
scroggo@google.com | aae9e27 | 2012-07-02 14:36:33 +0000 | [diff] [blame] | 74 | */ |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | private: |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 78 | typedef GM INHERITED; |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | ////////////////////////////////////////////////////////////////////////////// |
| 82 | |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 83 | static GM* MyFactory(void*) { return new ExtractBitmapGM; } |
| 84 | static GMRegistry reg(MyFactory); |
| 85 | |
| 86 | } |