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" |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 11 | #include "SkString.h" |
reed@google.com | 76f10a3 | 2014-02-05 15:32:21 +0000 | [diff] [blame] | 12 | #include "SkSurface.h" |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 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; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 19 | bitmap->allocN32Pixels(W, H); |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 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 | |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 28 | class ExtractBitmapGM : public GM { |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 29 | public: |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 30 | ExtractBitmapGM() {} |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 31 | |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 32 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 33 | SkString onShortName() override { |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 34 | return SkString("extractbitmap"); |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 35 | } |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 36 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 37 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 38 | return SkISize::Make(600, 600); |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 39 | } |
| 40 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 41 | void onDraw(SkCanvas* canvas) override { |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 42 | SkBitmap bitmap; |
| 43 | create_bitmap(&bitmap); |
| 44 | int x = bitmap.width() / 2; |
| 45 | int y = bitmap.height() / 2; |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 46 | |
| 47 | canvas->translate(SkIntToScalar(20), SkIntToScalar(20)); |
| 48 | |
| 49 | canvas->drawBitmap(bitmap, 0, 0); |
scroggo@google.com | 6ea165d | 2012-07-03 14:52:08 +0000 | [diff] [blame] | 50 | |
| 51 | { |
| 52 | // Do some subset drawing. This will test that an SkGPipe properly |
| 53 | // handles the case where bitmaps share a pixelref |
| 54 | // Draw the bottom right fourth of the bitmap over the top left |
| 55 | SkBitmap subset; |
| 56 | bitmap.extractSubset(&subset, SkIRect::MakeXYWH(x, y, x, y)); |
| 57 | canvas->drawBitmap(subset, 0, 0); |
| 58 | // Draw the top left corner over the bottom right |
| 59 | bitmap.extractSubset(&subset, SkIRect::MakeWH(x, y)); |
| 60 | canvas->drawBitmap(subset, SkIntToScalar(x), SkIntToScalar(y)); |
| 61 | // Draw a subset which has the same height and pixelref offset but a |
| 62 | // different width |
| 63 | bitmap.extractSubset(&subset, SkIRect::MakeWH(x, bitmap.height())); |
| 64 | SkAutoCanvasRestore autoRestore(canvas, true); |
| 65 | canvas->translate(0, SkIntToScalar(bitmap.height() + 20)); |
| 66 | canvas->drawBitmap(subset, 0, 0); |
| 67 | // Now draw a subet which has the same width and pixelref offset but |
| 68 | // a different height |
| 69 | bitmap.extractSubset(&subset, SkIRect::MakeWH(bitmap.height(), y)); |
| 70 | canvas->translate(0, SkIntToScalar(bitmap.height() + 20)); |
| 71 | canvas->drawBitmap(subset, 0, 0); |
| 72 | } |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 73 | } |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 74 | |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 75 | private: |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 76 | typedef GM INHERITED; |
junov@google.com | 61d46a0 | 2011-07-28 13:34:31 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | ////////////////////////////////////////////////////////////////////////////// |
| 80 | |
scroggo@google.com | 4f1f6bf | 2012-07-02 13:35:09 +0000 | [diff] [blame] | 81 | static GM* MyFactory(void*) { return new ExtractBitmapGM; } |
| 82 | static GMRegistry reg(MyFactory); |
| 83 | |
| 84 | } |