fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [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 "SkImage.h" |
| 11 | #include "SkImageSource.h" |
| 12 | |
| 13 | // This GM exercises the SkImageSource ImageFilter class. |
| 14 | |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 15 | static void fill_rect_filtered(SkCanvas* canvas, |
| 16 | const SkRect& clipRect, |
| 17 | sk_sp<SkImageFilter> filter) { |
| 18 | SkPaint paint; |
| 19 | paint.setImageFilter(std::move(filter)); |
| 20 | canvas->save(); |
| 21 | canvas->clipRect(clipRect); |
| 22 | canvas->drawPaint(paint); |
| 23 | canvas->restore(); |
| 24 | } |
| 25 | |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 26 | class ImageSourceGM : public skiagm::GM { |
| 27 | public: |
| 28 | ImageSourceGM() { } |
| 29 | |
| 30 | protected: |
| 31 | SkString onShortName() override { |
| 32 | return SkString("imagesource"); |
| 33 | } |
| 34 | |
| 35 | SkISize onISize() override { return SkISize::Make(500, 150); } |
| 36 | |
| 37 | void onOnceBeforeDraw() override { |
| 38 | SkBitmap bm = sk_tool_utils::create_string_bitmap(100, 100, 0xFFFFFFFF, 20, 70, 96, "e"); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 39 | fImage = SkImage::MakeFromBitmap(bm); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 40 | } |
| 41 | |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 42 | void onDraw(SkCanvas* canvas) override { |
| 43 | canvas->clear(SK_ColorBLACK); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 44 | |
| 45 | const SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30); |
| 46 | const SkRect dstRect = SkRect::MakeXYWH(0, 10, 60, 60); |
| 47 | const SkRect clipRect = SkRect::MakeXYWH(0, 0, 100, 100); |
| 48 | const SkRect bounds = SkRect::MakeIWH(fImage->width(), fImage->height()); |
| 49 | |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 50 | { |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 51 | // Draw an unscaled bitmap. |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 52 | sk_sp<SkImageFilter> imageSource(SkImageSource::Make(fImage)); |
| 53 | fill_rect_filtered(canvas, clipRect, std::move(imageSource)); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 54 | canvas->translate(SkIntToScalar(100), 0); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 55 | } |
| 56 | { |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 57 | // Draw an unscaled subset of the source bitmap (srcRect -> srcRect). |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 58 | sk_sp<SkImageFilter> imageSourceSrcRect( |
| 59 | SkImageSource::Make(fImage, srcRect, srcRect, kHigh_SkFilterQuality)); |
| 60 | fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRect)); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 61 | canvas->translate(SkIntToScalar(100), 0); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 62 | } |
| 63 | { |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 64 | // Draw a subset of the bitmap scaled to a destination rect (srcRect -> dstRect). |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 65 | sk_sp<SkImageFilter> imageSourceSrcRectDstRect( |
| 66 | SkImageSource::Make(fImage, srcRect, dstRect, kHigh_SkFilterQuality)); |
| 67 | fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRectDstRect)); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 68 | canvas->translate(SkIntToScalar(100), 0); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 69 | } |
| 70 | { |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 71 | // Draw the entire bitmap scaled to a destination rect (bounds -> dstRect). |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 72 | sk_sp<SkImageFilter> imageSourceDstRectOnly( |
| 73 | SkImageSource::Make(fImage, bounds, dstRect, kHigh_SkFilterQuality)); |
| 74 | fill_rect_filtered(canvas, clipRect, std::move(imageSourceDstRectOnly)); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 75 | canvas->translate(SkIntToScalar(100), 0); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | private: |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 80 | sk_sp<SkImage> fImage; |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 81 | typedef GM INHERITED; |
| 82 | }; |
| 83 | |
| 84 | /////////////////////////////////////////////////////////////////////////////// |
| 85 | |
| 86 | DEF_GM( return new ImageSourceGM; ) |