reed@google.com | 8926b16 | 2012-03-23 15:36:36 +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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 10 | #include "SkCanvas.h" |
| 11 | #include "SkColorFilter.h" |
| 12 | #include "SkColorPriv.h" |
| 13 | #include "SkShader.h" |
| 14 | |
| 15 | #include "SkBlurImageFilter.h" |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 16 | #include "SkColorFilterImageFilter.h" |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 17 | #include "SkDropShadowImageFilter.h" |
robertphillips | 4ba94e2 | 2016-04-04 12:07:47 -0700 | [diff] [blame] | 18 | #include "SkSpecialImage.h" |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 19 | |
| 20 | class FailImageFilter : public SkImageFilter { |
| 21 | public: |
reed | 7daaaa4 | 2014-08-21 10:53:34 -0700 | [diff] [blame] | 22 | class Registrar { |
| 23 | public: |
| 24 | Registrar() { |
| 25 | SkFlattenable::Register("FailImageFilter", |
mtklein | 3b37545 | 2016-04-04 14:57:19 -0700 | [diff] [blame] | 26 | FailImageFilter::CreateProc, |
| 27 | FailImageFilter::GetFlattenableType()); |
reed | 7daaaa4 | 2014-08-21 10:53:34 -0700 | [diff] [blame] | 28 | } |
| 29 | }; |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 30 | static sk_sp<SkImageFilter> Make() { |
| 31 | return sk_sp<SkImageFilter>(new FailImageFilter); |
| 32 | } |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 33 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 34 | SK_TO_STRING_OVERRIDE() |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 35 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(FailImageFilter) |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 36 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 37 | protected: |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 38 | FailImageFilter() : INHERITED(nullptr, 0, nullptr) {} |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 39 | |
robertphillips | 4ba94e2 | 2016-04-04 12:07:47 -0700 | [diff] [blame] | 40 | sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&, |
| 41 | SkIPoint* offset) const override { |
| 42 | return nullptr; |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 43 | } |
Matt Sarett | 62745a8 | 2017-04-17 11:57:29 -0400 | [diff] [blame] | 44 | sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override { |
| 45 | return nullptr; |
| 46 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 47 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 48 | private: |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 49 | typedef SkImageFilter INHERITED; |
| 50 | }; |
| 51 | |
reed | 7daaaa4 | 2014-08-21 10:53:34 -0700 | [diff] [blame] | 52 | static FailImageFilter::Registrar gReg0; |
| 53 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 54 | sk_sp<SkFlattenable> FailImageFilter::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 55 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0); |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 56 | return FailImageFilter::Make(); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 57 | } |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 58 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 59 | #ifndef SK_IGNORE_TO_STRING |
| 60 | void FailImageFilter::toString(SkString* str) const { |
| 61 | str->appendf("FailImageFilter: ("); |
| 62 | str->append(")"); |
| 63 | } |
| 64 | #endif |
| 65 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 66 | class IdentityImageFilter : public SkImageFilter { |
| 67 | public: |
reed | 7daaaa4 | 2014-08-21 10:53:34 -0700 | [diff] [blame] | 68 | class Registrar { |
| 69 | public: |
| 70 | Registrar() { |
| 71 | SkFlattenable::Register("IdentityImageFilter", |
mtklein | 3b37545 | 2016-04-04 14:57:19 -0700 | [diff] [blame] | 72 | IdentityImageFilter::CreateProc, |
| 73 | IdentityImageFilter::GetFlattenableType()); |
reed | 7daaaa4 | 2014-08-21 10:53:34 -0700 | [diff] [blame] | 74 | } |
| 75 | }; |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 76 | static sk_sp<SkImageFilter> Make(sk_sp<SkImageFilter> input) { |
| 77 | return sk_sp<SkImageFilter>(new IdentityImageFilter(std::move(input))); |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 78 | } |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 79 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 80 | SK_TO_STRING_OVERRIDE() |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 81 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(IdentityImageFilter) |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 82 | |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 83 | protected: |
robertphillips | 4ba94e2 | 2016-04-04 12:07:47 -0700 | [diff] [blame] | 84 | sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&, |
| 85 | SkIPoint* offset) const override { |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 86 | offset->set(0, 0); |
robertphillips | 4ba94e2 | 2016-04-04 12:07:47 -0700 | [diff] [blame] | 87 | return sk_ref_sp<SkSpecialImage>(source); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 88 | } |
Matt Sarett | 62745a8 | 2017-04-17 11:57:29 -0400 | [diff] [blame] | 89 | sk_sp<SkImageFilter> onMakeColorSpace(SkColorSpaceXformer*) const override { |
| 90 | return sk_ref_sp(const_cast<IdentityImageFilter*>(this)); |
| 91 | } |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 92 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 93 | private: |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 94 | IdentityImageFilter(sk_sp<SkImageFilter> input) : INHERITED(&input, 1, nullptr) {} |
| 95 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 96 | typedef SkImageFilter INHERITED; |
| 97 | }; |
| 98 | |
reed | 7daaaa4 | 2014-08-21 10:53:34 -0700 | [diff] [blame] | 99 | static IdentityImageFilter::Registrar gReg1; |
| 100 | |
reed | 60c9b58 | 2016-04-03 09:11:13 -0700 | [diff] [blame] | 101 | sk_sp<SkFlattenable> IdentityImageFilter::CreateProc(SkReadBuffer& buffer) { |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 102 | SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1); |
robertphillips | 43c2ad4 | 2016-04-04 05:05:11 -0700 | [diff] [blame] | 103 | return IdentityImageFilter::Make(common.getInput(0)); |
reed | 9fa60da | 2014-08-21 07:59:51 -0700 | [diff] [blame] | 104 | } |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 105 | |
robertphillips | f3f5bad | 2014-12-19 13:49:15 -0800 | [diff] [blame] | 106 | #ifndef SK_IGNORE_TO_STRING |
| 107 | void IdentityImageFilter::toString(SkString* str) const { |
| 108 | str->appendf("IdentityImageFilter: ("); |
| 109 | str->append(")"); |
| 110 | } |
| 111 | #endif |
| 112 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 113 | /////////////////////////////////////////////////////////////////////////////// |
| 114 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 115 | static void draw_paint(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 116 | SkPaint paint; |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 117 | paint.setImageFilter(std::move(imf)); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 118 | paint.setColor(SK_ColorGREEN); |
| 119 | canvas->save(); |
| 120 | canvas->clipRect(r); |
| 121 | canvas->drawPaint(paint); |
| 122 | canvas->restore(); |
| 123 | } |
| 124 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 125 | static void draw_line(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 126 | SkPaint paint; |
| 127 | paint.setColor(SK_ColorBLUE); |
| 128 | paint.setImageFilter(imf); |
| 129 | paint.setStrokeWidth(r.width()/10); |
| 130 | canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, paint); |
| 131 | } |
| 132 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 133 | static void draw_rect(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 134 | SkPaint paint; |
| 135 | paint.setColor(SK_ColorYELLOW); |
| 136 | paint.setImageFilter(imf); |
| 137 | SkRect rr(r); |
| 138 | rr.inset(r.width()/10, r.height()/10); |
| 139 | canvas->drawRect(rr, paint); |
| 140 | } |
| 141 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 142 | static void draw_path(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 143 | SkPaint paint; |
| 144 | paint.setColor(SK_ColorMAGENTA); |
| 145 | paint.setImageFilter(imf); |
| 146 | paint.setAntiAlias(true); |
| 147 | canvas->drawCircle(r.centerX(), r.centerY(), r.width()*2/5, paint); |
| 148 | } |
| 149 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 150 | static void draw_text(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 151 | SkPaint paint; |
| 152 | paint.setImageFilter(imf); |
| 153 | paint.setColor(SK_ColorCYAN); |
| 154 | paint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 155 | sk_tool_utils::set_portable_typeface(&paint); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 156 | paint.setTextSize(r.height()/2); |
| 157 | paint.setTextAlign(SkPaint::kCenter_Align); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 158 | canvas->drawString("Text", r.centerX(), r.centerY(), paint); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 159 | } |
| 160 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 161 | static void draw_bitmap(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 162 | SkPaint paint; |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 163 | paint.setImageFilter(std::move(imf)); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 164 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 165 | SkIRect bounds; |
| 166 | r.roundOut(&bounds); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 167 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 168 | SkBitmap bm; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 169 | bm.allocN32Pixels(bounds.width(), bounds.height()); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 170 | bm.eraseColor(SK_ColorTRANSPARENT); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 171 | SkCanvas c(bm); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 172 | draw_path(&c, r, nullptr); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 173 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 174 | canvas->drawBitmap(bm, 0, 0, &paint); |
| 175 | } |
| 176 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 177 | /////////////////////////////////////////////////////////////////////////////// |
| 178 | |
| 179 | class ImageFiltersBaseGM : public skiagm::GM { |
| 180 | public: |
| 181 | ImageFiltersBaseGM () {} |
| 182 | |
| 183 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 184 | SkString onShortName() override { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 185 | return SkString("imagefiltersbase"); |
| 186 | } |
| 187 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 188 | SkISize onISize() override { return SkISize::Make(700, 500); } |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 189 | |
| 190 | void draw_frame(SkCanvas* canvas, const SkRect& r) { |
| 191 | SkPaint paint; |
| 192 | paint.setStyle(SkPaint::kStroke_Style); |
| 193 | paint.setColor(SK_ColorRED); |
| 194 | canvas->drawRect(r, paint); |
| 195 | } |
| 196 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 197 | void onDraw(SkCanvas* canvas) override { |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 198 | void (*drawProc[])(SkCanvas*, const SkRect&, sk_sp<SkImageFilter>) = { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 199 | draw_paint, |
| 200 | draw_line, draw_rect, draw_path, draw_text, |
| 201 | draw_bitmap, |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 202 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 203 | |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 204 | auto cf = SkColorFilter::MakeModeFilter(SK_ColorRED, SkBlendMode::kSrcIn); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 205 | sk_sp<SkImageFilter> filters[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 206 | nullptr, |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 207 | IdentityImageFilter::Make(nullptr), |
| 208 | FailImageFilter::Make(), |
| 209 | SkColorFilterImageFilter::Make(std::move(cf), nullptr), |
| 210 | SkBlurImageFilter::Make(12.0f, 0.0f, nullptr), |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 211 | SkDropShadowImageFilter::Make( |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 212 | 10.0f, 5.0f, 3.0f, 3.0f, SK_ColorBLUE, |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 213 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, |
| 214 | nullptr), |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 215 | }; |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 216 | |
| 217 | SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64)); |
| 218 | SkScalar MARGIN = SkIntToScalar(16); |
| 219 | SkScalar DX = r.width() + MARGIN; |
| 220 | SkScalar DY = r.height() + MARGIN; |
| 221 | |
| 222 | canvas->translate(MARGIN, MARGIN); |
| 223 | for (size_t i = 0; i < SK_ARRAY_COUNT(drawProc); ++i) { |
| 224 | canvas->save(); |
| 225 | for (size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) { |
| 226 | drawProc[i](canvas, r, filters[j]); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 227 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 228 | draw_frame(canvas, r); |
| 229 | canvas->translate(0, DY); |
| 230 | } |
| 231 | canvas->restore(); |
| 232 | canvas->translate(DX, 0); |
| 233 | } |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | private: |
| 237 | typedef GM INHERITED; |
| 238 | }; |
reed | 8dc0ccb | 2015-03-20 06:32:52 -0700 | [diff] [blame] | 239 | DEF_GM( return new ImageFiltersBaseGM; ) |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 240 | |
| 241 | /////////////////////////////////////////////////////////////////////////////// |
| 242 | |
reed | 8dc0ccb | 2015-03-20 06:32:52 -0700 | [diff] [blame] | 243 | /* |
| 244 | * Want to test combos of filter and LCD text, to be sure we disable LCD in the presence of |
| 245 | * a filter. |
| 246 | */ |
| 247 | class ImageFiltersTextBaseGM : public skiagm::GM { |
| 248 | SkString fSuffix; |
| 249 | public: |
| 250 | ImageFiltersTextBaseGM(const char suffix[]) : fSuffix(suffix) {} |
| 251 | |
| 252 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 253 | SkString onShortName() override { |
reed | 8dc0ccb | 2015-03-20 06:32:52 -0700 | [diff] [blame] | 254 | SkString name; |
| 255 | name.printf("%s_%s", "textfilter", fSuffix.c_str()); |
| 256 | return name; |
| 257 | } |
| 258 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 259 | SkISize onISize() override { return SkISize::Make(512, 342); } |
reed | 8dc0ccb | 2015-03-20 06:32:52 -0700 | [diff] [blame] | 260 | |
| 261 | void drawWaterfall(SkCanvas* canvas, const SkPaint& origPaint) { |
| 262 | const uint32_t flags[] = { |
| 263 | 0, |
| 264 | SkPaint::kAntiAlias_Flag, |
| 265 | SkPaint::kAntiAlias_Flag | SkPaint::kLCDRenderText_Flag, |
| 266 | }; |
| 267 | SkPaint paint(origPaint); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 268 | sk_tool_utils::set_portable_typeface(&paint); |
reed | 8dc0ccb | 2015-03-20 06:32:52 -0700 | [diff] [blame] | 269 | paint.setTextSize(30); |
| 270 | |
| 271 | SkAutoCanvasRestore acr(canvas, true); |
| 272 | for (size_t i = 0; i < SK_ARRAY_COUNT(flags); ++i) { |
| 273 | paint.setFlags(flags[i]); |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 274 | canvas->drawString("Hamburgefon", 0, 0, paint); |
reed | 8dc0ccb | 2015-03-20 06:32:52 -0700 | [diff] [blame] | 275 | canvas->translate(0, 40); |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | virtual void installFilter(SkPaint* paint) = 0; |
| 280 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 281 | void onDraw(SkCanvas* canvas) override { |
reed | 8dc0ccb | 2015-03-20 06:32:52 -0700 | [diff] [blame] | 282 | SkPaint paint; |
| 283 | |
| 284 | canvas->translate(20, 40); |
| 285 | |
| 286 | for (int doSaveLayer = 0; doSaveLayer <= 1; ++doSaveLayer) { |
| 287 | SkAutoCanvasRestore acr(canvas, true); |
| 288 | for (int useFilter = 0; useFilter <= 1; ++useFilter) { |
| 289 | SkAutoCanvasRestore acr2(canvas, true); |
| 290 | |
| 291 | SkPaint paint; |
| 292 | if (useFilter) { |
| 293 | this->installFilter(&paint); |
| 294 | } |
| 295 | if (doSaveLayer) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 296 | canvas->saveLayer(nullptr, &paint); |
| 297 | paint.setImageFilter(nullptr); |
reed | 8dc0ccb | 2015-03-20 06:32:52 -0700 | [diff] [blame] | 298 | } |
| 299 | this->drawWaterfall(canvas, paint); |
| 300 | |
| 301 | acr2.restore(); |
| 302 | canvas->translate(250, 0); |
| 303 | } |
| 304 | acr.restore(); |
| 305 | canvas->translate(0, 200); |
| 306 | } |
| 307 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 308 | |
reed | 8dc0ccb | 2015-03-20 06:32:52 -0700 | [diff] [blame] | 309 | private: |
| 310 | typedef GM INHERITED; |
| 311 | }; |
| 312 | |
| 313 | class ImageFiltersText_IF : public ImageFiltersTextBaseGM { |
| 314 | public: |
| 315 | ImageFiltersText_IF() : ImageFiltersTextBaseGM("image") {} |
| 316 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 317 | void installFilter(SkPaint* paint) override { |
robertphillips | 6e7025a | 2016-04-04 04:31:25 -0700 | [diff] [blame] | 318 | paint->setImageFilter(SkBlurImageFilter::Make(1.5f, 1.5f, nullptr)); |
reed | 8dc0ccb | 2015-03-20 06:32:52 -0700 | [diff] [blame] | 319 | } |
| 320 | }; |
| 321 | DEF_GM( return new ImageFiltersText_IF; ) |
| 322 | |
| 323 | class ImageFiltersText_CF : public ImageFiltersTextBaseGM { |
| 324 | public: |
| 325 | ImageFiltersText_CF() : ImageFiltersTextBaseGM("color") {} |
| 326 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 327 | void installFilter(SkPaint* paint) override { |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 328 | paint->setColorFilter(SkColorFilter::MakeModeFilter(SK_ColorBLUE, SkBlendMode::kSrcIn)); |
reed | 8dc0ccb | 2015-03-20 06:32:52 -0700 | [diff] [blame] | 329 | } |
| 330 | }; |
| 331 | DEF_GM( return new ImageFiltersText_CF; ) |