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