reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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 | #include "SkCanvas.h" |
| 10 | #include "SkColorFilter.h" |
| 11 | #include "SkColorPriv.h" |
| 12 | #include "SkShader.h" |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 13 | |
| 14 | #include "SkBlurImageFilter.h" |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 15 | #include "SkColorFilterImageFilter.h" |
commit-bot@chromium.org | 1c4c9ef | 2013-08-07 00:07:51 +0000 | [diff] [blame] | 16 | #include "SkComposeImageFilter.h" |
senorblanco@chromium.org | 4a9a612 | 2012-12-04 14:18:50 +0000 | [diff] [blame] | 17 | #include "SkMergeImageFilter.h" |
senorblanco@chromium.org | 9daf96a | 2012-11-06 20:29:39 +0000 | [diff] [blame] | 18 | #include "SkOffsetImageFilter.h" |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 19 | #include "SkTestImageFilters.h" |
| 20 | |
| 21 | #define FILTER_WIDTH SkIntToScalar(150) |
| 22 | #define FILTER_HEIGHT SkIntToScalar(200) |
| 23 | |
robertphillips | 491fb17 | 2016-03-30 12:32:58 -0700 | [diff] [blame] | 24 | static SkImageFilter* make0() { |
| 25 | return SkDownSampleImageFilter::Create(SK_Scalar1 / 5); |
| 26 | } |
| 27 | |
| 28 | static SkImageFilter* make1() { |
| 29 | return SkOffsetImageFilter::Create(SkIntToScalar(16), SkIntToScalar(16)); |
| 30 | } |
| 31 | |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 32 | static SkImageFilter* make2() { |
robertphillips | 491fb17 | 2016-03-30 12:32:58 -0700 | [diff] [blame] | 33 | sk_sp<SkColorFilter> cf(SkColorFilter::MakeModeFilter(SK_ColorBLUE, SkXfermode::kSrcIn_Mode)); |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 34 | return SkColorFilterImageFilter::Create(cf.get()); |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 35 | } |
robertphillips | 491fb17 | 2016-03-30 12:32:58 -0700 | [diff] [blame] | 36 | |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 37 | static SkImageFilter* make3() { |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 38 | return SkBlurImageFilter::Create(8, 0); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static SkImageFilter* make4() { |
robertphillips | 491fb17 | 2016-03-30 12:32:58 -0700 | [diff] [blame] | 42 | sk_sp<SkImageFilter> outer(SkOffsetImageFilter::Create(SkIntToScalar(16), SkIntToScalar(16))); |
| 43 | sk_sp<SkImageFilter> inner(SkDownSampleImageFilter::Create(SK_Scalar1 / 5)); |
| 44 | return SkComposeImageFilter::Make(std::move(outer), std::move(inner)).release(); |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 45 | } |
robertphillips | 491fb17 | 2016-03-30 12:32:58 -0700 | [diff] [blame] | 46 | |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 47 | static SkImageFilter* make5() { |
robertphillips | 491fb17 | 2016-03-30 12:32:58 -0700 | [diff] [blame] | 48 | sk_sp<SkImageFilter> first(SkOffsetImageFilter::Create(SkIntToScalar(16), SkIntToScalar(16))); |
| 49 | sk_sp<SkImageFilter> second(SkDownSampleImageFilter::Create(SK_Scalar1 / 5)); |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame^] | 50 | return SkMergeImageFilter::Make(std::move(first), std::move(second)).release(); |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 51 | } |
| 52 | |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 53 | static SkImageFilter* make6() { |
robertphillips | 491fb17 | 2016-03-30 12:32:58 -0700 | [diff] [blame] | 54 | sk_sp<SkImageFilter> outer(SkOffsetImageFilter::Create(SkIntToScalar(16), SkIntToScalar(16))); |
| 55 | sk_sp<SkImageFilter> inner(SkDownSampleImageFilter::Create(SK_Scalar1 / 5)); |
| 56 | sk_sp<SkImageFilter> compose(SkComposeImageFilter::Make(std::move(outer), std::move(inner))); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 57 | |
robertphillips | 491fb17 | 2016-03-30 12:32:58 -0700 | [diff] [blame] | 58 | sk_sp<SkColorFilter> cf(SkColorFilter::MakeModeFilter(0x880000FF, SkXfermode::kSrcIn_Mode)); |
| 59 | sk_sp<SkImageFilter> blue(SkColorFilterImageFilter::Create(cf.get())); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 60 | |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame^] | 61 | return SkMergeImageFilter::Make(std::move(compose), std::move(blue)).release(); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 62 | } |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 63 | |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 64 | static SkImageFilter* make7() { |
robertphillips | 491fb17 | 2016-03-30 12:32:58 -0700 | [diff] [blame] | 65 | sk_sp<SkImageFilter> outer(SkOffsetImageFilter::Create(SkIntToScalar(16), SkIntToScalar(16))); |
| 66 | sk_sp<SkImageFilter> inner(make3()); |
| 67 | sk_sp<SkImageFilter> compose(SkComposeImageFilter::Make(std::move(outer), std::move(inner))); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 68 | |
robertphillips | 491fb17 | 2016-03-30 12:32:58 -0700 | [diff] [blame] | 69 | sk_sp<SkColorFilter> cf(SkColorFilter::MakeModeFilter(0x880000FF, SkXfermode::kSrcIn_Mode)); |
| 70 | sk_sp<SkImageFilter> blue(SkColorFilterImageFilter::Create(cf.get())); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 71 | |
robertphillips | 2238c9d | 2016-03-30 13:34:16 -0700 | [diff] [blame^] | 72 | return SkMergeImageFilter::Make(std::move(compose), std::move(blue)).release(); |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | static void draw0(SkCanvas* canvas) { |
| 76 | SkPaint p; |
| 77 | p.setAntiAlias(true); |
| 78 | SkRect r = SkRect::MakeWH(FILTER_WIDTH, FILTER_HEIGHT); |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 79 | r.inset(SK_Scalar1 * 12, SK_Scalar1 * 12); |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 80 | p.setColor(SK_ColorRED); |
| 81 | canvas->drawOval(r, p); |
| 82 | } |
| 83 | |
| 84 | class TestImageFiltersGM : public skiagm::GM { |
| 85 | public: |
| 86 | TestImageFiltersGM () {} |
| 87 | |
| 88 | protected: |
| 89 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 90 | SkString onShortName() override { |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 91 | return SkString("testimagefilters"); |
| 92 | } |
| 93 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 94 | SkISize onISize() override { return SkISize::Make(700, 460); } |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 95 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 96 | void onDraw(SkCanvas* canvas) override { |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 97 | // this->drawSizeBounds(canvas, 0xFFCCCCCC); |
| 98 | |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 99 | static SkImageFilter* (*gFilterProc[])() = { |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 100 | make0, make1, make2, make3, make4, make5, make6, make7 |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 101 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 102 | |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 103 | const SkRect bounds = SkRect::MakeWH(FILTER_WIDTH, FILTER_HEIGHT); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 104 | |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 105 | const SkScalar dx = bounds.width() * 8 / 7; |
| 106 | const SkScalar dy = bounds.height() * 8 / 7; |
| 107 | |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 108 | canvas->translate(SkIntToScalar(8), SkIntToScalar(8)); |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 109 | |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 110 | for (int i = 0; i < (int)SK_ARRAY_COUNT(gFilterProc); ++i) { |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 111 | int ix = i % 4; |
| 112 | int iy = i / 4; |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 113 | |
| 114 | SkAutoCanvasRestore acr(canvas, true); |
| 115 | canvas->translate(ix * dx, iy * dy); |
| 116 | |
| 117 | SkPaint p; |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 118 | p.setStyle(SkPaint::kStroke_Style); |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 119 | canvas->drawRect(bounds, p); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 120 | |
reed@google.com | 76dd277 | 2012-01-05 21:15:07 +0000 | [diff] [blame] | 121 | SkPaint paint; |
| 122 | paint.setImageFilter(gFilterProc[i]())->unref(); |
reed@google.com | 32d25b6 | 2011-12-20 16:19:00 +0000 | [diff] [blame] | 123 | canvas->saveLayer(&bounds, &paint); |
| 124 | draw0(canvas); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | private: |
| 129 | typedef GM INHERITED; |
| 130 | }; |
| 131 | |
| 132 | ////////////////////////////////////////////////////////////////////////////// |
| 133 | |
scroggo | 96f16e8 | 2015-12-10 13:31:59 -0800 | [diff] [blame] | 134 | DEF_GM( return new TestImageFiltersGM; ) |