epoger@google.com | fd03db0 | 2011-07-28 14:24:55 +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 | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 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" |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 13 | #include "SkSurface.h" |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 14 | |
| 15 | namespace skiagm { |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 16 | |
| 17 | static void create_bitmap(SkBitmap* bitmap) { |
| 18 | const int W = 100; |
| 19 | const int H = 100; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 20 | bitmap->allocN32Pixels(W, H); |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 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() {} |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 32 | |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 33 | protected: |
| 34 | // overrides from SkEventSink |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 35 | SkString onShortName() SK_OVERRIDE { |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 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 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 39 | SkISize onISize() SK_OVERRIDE { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 40 | return SkISize::Make(600, 600); |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 41 | } |
| 42 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 43 | 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; |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 48 | |
| 49 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 50 | |
| 51 | canvas->drawBitmap(bitmap, 0, 0); |
scroggo@google.com | 6ea165d | 2012-07-03 14:52:08 +0000 | [diff] [blame] | 52 | |
| 53 | { |
| 54 | // Do some subset drawing. This will test that an SkGPipe properly |
| 55 | // handles the case where bitmaps share a pixelref |
| 56 | // Draw the bottom right fourth of the bitmap over the top left |
| 57 | SkBitmap subset; |
| 58 | bitmap.extractSubset(&subset, SkIRect::MakeXYWH(x, y, x, y)); |
| 59 | canvas->drawBitmap(subset, 0, 0); |
| 60 | // Draw the top left corner over the bottom right |
| 61 | bitmap.extractSubset(&subset, SkIRect::MakeWH(x, y)); |
| 62 | canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y)); |
| 63 | // Draw a subset which has the same height and pixelref offset but a |
| 64 | // different width |
| 65 | bitmap.extractSubset(&subset, SkIRect::MakeWH(x, bitmap.height())); |
| 66 | SkAutoCanvasRestore autoRestore(canvas, true); |
| 67 | canvas->translate(0, SkIntToScalar(bitmap.height() + 20)); |
| 68 | canvas->drawBitmap(subset, 0, 0); |
| 69 | // Now draw a subet which has the same width and pixelref offset but |
| 70 | // a different height |
| 71 | bitmap.extractSubset(&subset, SkIRect::MakeWH(bitmap.height(), y)); |
| 72 | canvas->translate(0, SkIntToScalar(bitmap.height() + 20)); |
| 73 | canvas->drawBitmap(subset, 0, 0); |
| 74 | } |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 75 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 76 | |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 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 | } |