commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +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 | |
halcanary | 878fa02 | 2015-01-26 11:24:32 -0800 | [diff] [blame] | 8 | #include "SkColorFilterImageFilter.h" |
| 9 | #include "SkColorMatrixFilter.h" |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 10 | #include "SkImage.h" |
| 11 | #include "SkImageSource.h" |
halcanary | 878fa02 | 2015-01-26 11:24:32 -0800 | [diff] [blame] | 12 | #include "SkTileImageFilter.h" |
| 13 | #include "gm.h" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 14 | #include "sk_tool_utils.h" |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 15 | |
| 16 | #define WIDTH 400 |
senorblanco | f270029 | 2016-04-01 09:24:48 -0700 | [diff] [blame] | 17 | #define HEIGHT 200 |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 18 | #define MARGIN 12 |
| 19 | |
| 20 | namespace skiagm { |
| 21 | |
| 22 | class TileImageFilterGM : public GM { |
| 23 | public: |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 24 | TileImageFilterGM() { |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 25 | this->setBGColor(0xFF000000); |
| 26 | } |
| 27 | |
| 28 | protected: |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 29 | SkString onShortName() override { |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 30 | return SkString("tileimagefilter"); |
| 31 | } |
| 32 | |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 33 | SkISize onISize() override{ |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 34 | return SkISize::Make(WIDTH, HEIGHT); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 35 | } |
| 36 | |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 37 | void onOnceBeforeDraw() override { |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 38 | fBitmap = SkImage::MakeFromBitmap( |
| 39 | sk_tool_utils::create_string_bitmap(50, 50, 0xD000D000, 10, 45, 50, "e")); |
halcanary | 878fa02 | 2015-01-26 11:24:32 -0800 | [diff] [blame] | 40 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 41 | fCheckerboard = SkImage::MakeFromBitmap( |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 42 | sk_tool_utils::create_checkerboard_bitmap(80, 80, |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 43 | 0xFFA0A0A0, |
| 44 | 0xFF404040, |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 45 | 8)); |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 46 | } |
halcanary | 878fa02 | 2015-01-26 11:24:32 -0800 | [diff] [blame] | 47 | |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 48 | void onDraw(SkCanvas* canvas) override { |
senorblanco | 16b254a | 2015-04-09 11:13:24 -0700 | [diff] [blame] | 49 | canvas->clear(SK_ColorBLACK); |
senorblanco | a9fbd16 | 2016-01-11 14:09:09 -0800 | [diff] [blame] | 50 | SkPaint red; |
| 51 | red.setColor(SK_ColorRED); |
| 52 | red.setStyle(SkPaint::kStroke_Style); |
| 53 | SkPaint blue; |
| 54 | blue.setColor(SK_ColorBLUE); |
| 55 | blue.setStyle(SkPaint::kStroke_Style); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 56 | |
| 57 | int x = 0, y = 0; |
| 58 | for (size_t i = 0; i < 4; i++) { |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 59 | sk_sp<SkImage> image = (i & 0x01) ? fCheckerboard : fBitmap; |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 60 | SkRect srcRect = SkRect::MakeXYWH(SkIntToScalar(image->width()/4), |
| 61 | SkIntToScalar(image->height()/4), |
| 62 | SkIntToScalar(image->width()/(i+1)), |
| 63 | SkIntToScalar(image->height()/(i+1))); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 64 | SkRect dstRect = SkRect::MakeXYWH(SkIntToScalar(i * 8), |
| 65 | SkIntToScalar(i * 4), |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 66 | SkIntToScalar(image->width() - i * 12), |
| 67 | SkIntToScalar(image->height()) - i * 12); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 68 | sk_sp<SkImageFilter> tileInput(SkImageSource::Make(image)); |
robertphillips | 534c270 | 2016-04-15 07:57:40 -0700 | [diff] [blame] | 69 | sk_sp<SkImageFilter> filter(SkTileImageFilter::Make(srcRect, |
| 70 | dstRect, |
| 71 | std::move(tileInput))); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 72 | canvas->save(); |
| 73 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 74 | SkPaint paint; |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 75 | paint.setImageFilter(std::move(filter)); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 76 | canvas->drawImage(fBitmap.get(), 0, 0, &paint); |
senorblanco | a9fbd16 | 2016-01-11 14:09:09 -0800 | [diff] [blame] | 77 | canvas->drawRect(srcRect, red); |
| 78 | canvas->drawRect(dstRect, blue); |
senorblanco@chromium.org | 6776b82 | 2014-01-03 21:48:22 +0000 | [diff] [blame] | 79 | canvas->restore(); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 80 | x += image->width() + MARGIN; |
| 81 | if (x + image->width() > WIDTH) { |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 82 | x = 0; |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 83 | y += image->height() + MARGIN; |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 86 | |
| 87 | SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0, |
| 88 | 0, SK_Scalar1, 0, 0, 0, |
| 89 | 0, 0, SK_Scalar1, 0, 0, |
| 90 | 0, 0, 0, SK_Scalar1, 0 }; |
| 91 | |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 92 | SkRect srcRect = SkRect::MakeWH(SkIntToScalar(fBitmap->width()), |
| 93 | SkIntToScalar(fBitmap->height())); |
| 94 | SkRect dstRect = SkRect::MakeWH(SkIntToScalar(fBitmap->width() * 2), |
| 95 | SkIntToScalar(fBitmap->height() * 2)); |
robertphillips | 534c270 | 2016-04-15 07:57:40 -0700 | [diff] [blame] | 96 | sk_sp<SkImageFilter> tile(SkTileImageFilter::Make(srcRect, dstRect, nullptr)); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 97 | sk_sp<SkColorFilter> cf(SkColorFilter::MakeMatrixFilterRowMajor255(matrix)); |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 98 | |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 99 | SkPaint paint; |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 100 | paint.setImageFilter(SkColorFilterImageFilter::Make(std::move(cf), std::move(tile))); |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 101 | canvas->save(); |
| 102 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 103 | canvas->clipRect(dstRect); |
| 104 | canvas->saveLayer(&dstRect, &paint); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 105 | canvas->drawImage(fBitmap.get(), 0, 0); |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 106 | canvas->restore(); |
senorblanco | a9fbd16 | 2016-01-11 14:09:09 -0800 | [diff] [blame] | 107 | canvas->drawRect(srcRect, red); |
| 108 | canvas->drawRect(dstRect, blue); |
senorblanco@chromium.org | 0a5c233 | 2014-04-29 15:20:39 +0000 | [diff] [blame] | 109 | canvas->restore(); |
senorblanco | f270029 | 2016-04-01 09:24:48 -0700 | [diff] [blame] | 110 | |
| 111 | canvas->translate(0, SkIntToScalar(100)); |
| 112 | |
| 113 | srcRect = SkRect::MakeXYWH(0, 0, 50, 50); |
| 114 | dstRect = SkRect::MakeXYWH(0, 0, 100, 100); |
| 115 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(5, 5, 40, 40)); |
| 116 | sk_sp<SkColorFilter> greenCF = SkColorFilter::MakeModeFilter(SK_ColorGREEN, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 117 | SkBlendMode::kSrc); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 118 | sk_sp<SkImageFilter> green(SkColorFilterImageFilter::Make(std::move(greenCF), |
| 119 | nullptr, |
| 120 | &cropRect)); |
robertphillips | 534c270 | 2016-04-15 07:57:40 -0700 | [diff] [blame] | 121 | tile = SkTileImageFilter::Make(srcRect, dstRect, std::move(green)); |
senorblanco | f270029 | 2016-04-01 09:24:48 -0700 | [diff] [blame] | 122 | paint.setColor(SK_ColorRED); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 123 | paint.setImageFilter(std::move(tile)); |
senorblanco | f270029 | 2016-04-01 09:24:48 -0700 | [diff] [blame] | 124 | canvas->drawRect(dstRect, paint); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 125 | } |
| 126 | private: |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 127 | sk_sp<SkImage> fBitmap, fCheckerboard; |
robertphillips | 6319518 | 2015-06-08 06:21:14 -0700 | [diff] [blame] | 128 | |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 129 | typedef GM INHERITED; |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | ////////////////////////////////////////////////////////////////////////////// |
| 133 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 134 | DEF_GM(return new TileImageFilterGM;) |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 135 | } |