senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | */ |
| 7 | |
| 8 | #include "gm.h" |
| 9 | |
| 10 | #include "SkBitmapSource.h" |
| 11 | |
| 12 | // This GM exercises the SkBitmapSource ImageFilter class. |
| 13 | |
| 14 | class BitmapSourceGM : public skiagm::GM { |
| 15 | public: |
| 16 | BitmapSourceGM() { |
| 17 | } |
| 18 | |
| 19 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 20 | SkString onShortName() override { |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 21 | return SkString("bitmapsource"); |
| 22 | } |
| 23 | |
| 24 | void makeBitmap() { |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 25 | fBitmap.allocN32Pixels(100, 100); |
commit-bot@chromium.org | c3bd8af | 2014-02-13 17:14:46 +0000 | [diff] [blame] | 26 | SkCanvas canvas(fBitmap); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 27 | canvas.clear(0x00000000); |
| 28 | SkPaint paint; |
| 29 | paint.setAntiAlias(true); |
caryclark | 5fb6bd4 | 2014-06-23 11:25:00 -0700 | [diff] [blame] | 30 | sk_tool_utils::set_portable_typeface(&paint); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 31 | paint.setColor(0xFFFFFFFF); |
| 32 | paint.setTextSize(SkIntToScalar(96)); |
| 33 | const char* str = "e"; |
| 34 | canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint); |
| 35 | } |
| 36 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 37 | SkISize onISize() override { return SkISize::Make(500, 150); } |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 38 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 39 | void onOnceBeforeDraw() override { |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 40 | this->makeBitmap(); |
| 41 | } |
| 42 | |
| 43 | static void fillRectFiltered(SkCanvas* canvas, const SkRect& clipRect, SkImageFilter* filter) { |
| 44 | SkPaint paint; |
| 45 | paint.setImageFilter(filter); |
| 46 | canvas->save(); |
| 47 | canvas->clipRect(clipRect); |
| 48 | canvas->drawPaint(paint); |
| 49 | canvas->restore(); |
| 50 | } |
| 51 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame^] | 52 | void onDraw(SkCanvas* canvas) override { |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 53 | canvas->clear(0x00000000); |
| 54 | { |
| 55 | SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30); |
| 56 | SkRect dstRect = SkRect::MakeXYWH(0, 10, 60, 60); |
| 57 | SkRect clipRect = SkRect::MakeXYWH(0, 0, 100, 100); |
| 58 | SkRect bounds; |
| 59 | fBitmap.getBounds(&bounds); |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 60 | SkAutoTUnref<SkImageFilter> bitmapSource(SkBitmapSource::Create(fBitmap)); |
| 61 | SkAutoTUnref<SkImageFilter> bitmapSourceSrcRect(SkBitmapSource::Create(fBitmap, srcRect, srcRect)); |
| 62 | SkAutoTUnref<SkImageFilter> bitmapSourceSrcRectDstRect(SkBitmapSource::Create(fBitmap, srcRect, dstRect)); |
| 63 | SkAutoTUnref<SkImageFilter> bitmapSourceDstRectOnly(SkBitmapSource::Create(fBitmap, bounds, dstRect)); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 64 | |
| 65 | // Draw an unscaled bitmap. |
| 66 | fillRectFiltered(canvas, clipRect, bitmapSource); |
| 67 | canvas->translate(SkIntToScalar(100), 0); |
| 68 | |
| 69 | // Draw an unscaled subset of the source bitmap (srcRect -> srcRect). |
| 70 | fillRectFiltered(canvas, clipRect, bitmapSourceSrcRect); |
| 71 | canvas->translate(SkIntToScalar(100), 0); |
| 72 | |
| 73 | // Draw a subset of the bitmap scaled to a destination rect (srcRect -> dstRect). |
| 74 | fillRectFiltered(canvas, clipRect, bitmapSourceSrcRectDstRect); |
| 75 | canvas->translate(SkIntToScalar(100), 0); |
| 76 | |
| 77 | // Draw the entire bitmap scaled to a destination rect (bounds -> dstRect). |
| 78 | fillRectFiltered(canvas, clipRect, bitmapSourceDstRectOnly); |
| 79 | canvas->translate(SkIntToScalar(100), 0); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | SkBitmap fBitmap; |
| 85 | typedef GM INHERITED; |
| 86 | }; |
| 87 | |
| 88 | /////////////////////////////////////////////////////////////////////////////// |
| 89 | |
| 90 | DEF_GM( return new BitmapSourceGM; ) |