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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBitmap.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkFilterQuality.h" |
| 13 | #include "include/core/SkImage.h" |
| 14 | #include "include/core/SkImageFilter.h" |
| 15 | #include "include/core/SkPaint.h" |
| 16 | #include "include/core/SkRect.h" |
| 17 | #include "include/core/SkRefCnt.h" |
| 18 | #include "include/core/SkScalar.h" |
| 19 | #include "include/core/SkSize.h" |
| 20 | #include "include/core/SkString.h" |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 21 | #include "include/effects/SkImageFilters.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "tools/ToolUtils.h" |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 23 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 24 | #include <utility> |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 25 | |
| 26 | // This GM exercises the SkImageSource ImageFilter class. |
| 27 | |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 28 | static void fill_rect_filtered(SkCanvas* canvas, |
| 29 | const SkRect& clipRect, |
| 30 | sk_sp<SkImageFilter> filter) { |
| 31 | SkPaint paint; |
| 32 | paint.setImageFilter(std::move(filter)); |
| 33 | canvas->save(); |
| 34 | canvas->clipRect(clipRect); |
| 35 | canvas->drawPaint(paint); |
| 36 | canvas->restore(); |
| 37 | } |
| 38 | |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 39 | class ImageSourceGM : public skiagm::GM { |
| 40 | public: |
| 41 | ImageSourceGM() { } |
| 42 | |
| 43 | protected: |
| 44 | SkString onShortName() override { |
| 45 | return SkString("imagesource"); |
| 46 | } |
| 47 | |
| 48 | SkISize onISize() override { return SkISize::Make(500, 150); } |
| 49 | |
| 50 | void onOnceBeforeDraw() override { |
Mike Reed | ac9f0c9 | 2020-12-23 10:11:33 -0500 | [diff] [blame] | 51 | fImage = ToolUtils::create_string_image(100, 100, 0xFFFFFFFF, 20, 70, 96, "e"); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 52 | } |
| 53 | |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 54 | void onDraw(SkCanvas* canvas) override { |
| 55 | canvas->clear(SK_ColorBLACK); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 56 | |
| 57 | const SkRect srcRect = SkRect::MakeXYWH(20, 20, 30, 30); |
| 58 | const SkRect dstRect = SkRect::MakeXYWH(0, 10, 60, 60); |
| 59 | const SkRect clipRect = SkRect::MakeXYWH(0, 0, 100, 100); |
| 60 | const SkRect bounds = SkRect::MakeIWH(fImage->width(), fImage->height()); |
Mike Reed | 708dc4f | 2021-02-13 21:04:58 -0500 | [diff] [blame] | 61 | const SkSamplingOptions sampling({1/3.0f, 1/3.0f}); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 62 | |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 63 | { |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 64 | // Draw an unscaled bitmap. |
Michael Ludwig | 898bbfa | 2019-08-02 15:21:23 -0400 | [diff] [blame] | 65 | sk_sp<SkImageFilter> imageSource(SkImageFilters::Image(fImage)); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 66 | fill_rect_filtered(canvas, clipRect, std::move(imageSource)); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 67 | canvas->translate(SkIntToScalar(100), 0); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 68 | } |
| 69 | { |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 70 | // Draw an unscaled subset of the source bitmap (srcRect -> srcRect). |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 71 | sk_sp<SkImageFilter> imageSourceSrcRect( |
Mike Reed | 708dc4f | 2021-02-13 21:04:58 -0500 | [diff] [blame] | 72 | SkImageFilters::Image(fImage, srcRect, srcRect, sampling)); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 73 | fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRect)); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 74 | canvas->translate(SkIntToScalar(100), 0); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 75 | } |
| 76 | { |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 77 | // Draw a subset of the bitmap scaled to a destination rect (srcRect -> dstRect). |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 78 | sk_sp<SkImageFilter> imageSourceSrcRectDstRect( |
Mike Reed | 708dc4f | 2021-02-13 21:04:58 -0500 | [diff] [blame] | 79 | SkImageFilters::Image(fImage, srcRect, dstRect, sampling)); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 80 | fill_rect_filtered(canvas, clipRect, std::move(imageSourceSrcRectDstRect)); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 81 | canvas->translate(SkIntToScalar(100), 0); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 82 | } |
| 83 | { |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 84 | // Draw the entire bitmap scaled to a destination rect (bounds -> dstRect). |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 85 | sk_sp<SkImageFilter> imageSourceDstRectOnly( |
Mike Reed | 708dc4f | 2021-02-13 21:04:58 -0500 | [diff] [blame] | 86 | SkImageFilters::Image(fImage, bounds, dstRect, sampling)); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 87 | fill_rect_filtered(canvas, clipRect, std::move(imageSourceDstRectOnly)); |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 88 | canvas->translate(SkIntToScalar(100), 0); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | private: |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 93 | sk_sp<SkImage> fImage; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 94 | using INHERITED = GM; |
fmalita | cd56f81 | 2015-09-14 13:31:18 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | /////////////////////////////////////////////////////////////////////////////// |
| 98 | |
| 99 | DEF_GM( return new ImageSourceGM; ) |