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