senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +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 | |
| 8 | #include "gm.h" |
| 9 | #include "SkColorFilter.h" |
| 10 | |
| 11 | #include "SkColorFilterImageFilter.h" |
| 12 | #include "SkDropShadowImageFilter.h" |
| 13 | |
| 14 | /////////////////////////////////////////////////////////////////////////////// |
| 15 | |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 16 | static void draw_paint(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) { |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 17 | SkPaint paint; |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 18 | paint.setImageFilter(std::move(imf)); |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 19 | paint.setColor(SK_ColorBLACK); |
| 20 | canvas->save(); |
| 21 | canvas->clipRect(r); |
| 22 | canvas->drawPaint(paint); |
| 23 | canvas->restore(); |
| 24 | } |
| 25 | |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 26 | static void draw_path(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) { |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 27 | SkPaint paint; |
| 28 | paint.setColor(SK_ColorGREEN); |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 29 | paint.setImageFilter(std::move(imf)); |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 30 | paint.setAntiAlias(true); |
| 31 | canvas->save(); |
| 32 | canvas->clipRect(r); |
| 33 | canvas->drawCircle(r.centerX(), r.centerY(), r.width()/3, paint); |
| 34 | canvas->restore(); |
| 35 | } |
| 36 | |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 37 | static void draw_text(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) { |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 38 | SkPaint paint; |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 39 | paint.setImageFilter(std::move(imf)); |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 40 | paint.setColor(SK_ColorGREEN); |
| 41 | paint.setAntiAlias(true); |
caryclark | 1818acb | 2015-07-24 12:09:25 -0700 | [diff] [blame] | 42 | sk_tool_utils::set_portable_typeface(&paint); |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 43 | paint.setTextSize(r.height()/2); |
| 44 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 45 | canvas->save(); |
| 46 | canvas->clipRect(r); |
| 47 | canvas->drawText("Text", 4, r.centerX(), r.centerY(), paint); |
| 48 | canvas->restore(); |
| 49 | } |
| 50 | |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 51 | static void draw_bitmap(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) { |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 52 | SkPaint paint; |
| 53 | |
| 54 | SkIRect bounds; |
| 55 | r.roundOut(&bounds); |
| 56 | |
| 57 | SkBitmap bm; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 58 | bm.allocN32Pixels(bounds.width(), bounds.height()); |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 59 | bm.eraseColor(SK_ColorTRANSPARENT); |
| 60 | SkCanvas c(bm); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 61 | draw_path(&c, r, nullptr); |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 62 | |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 63 | paint.setImageFilter(std::move(imf)); |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 64 | canvas->save(); |
| 65 | canvas->clipRect(r); |
| 66 | canvas->drawBitmap(bm, 0, 0, &paint); |
| 67 | canvas->restore(); |
| 68 | } |
| 69 | |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 70 | /////////////////////////////////////////////////////////////////////////////// |
| 71 | |
| 72 | class DropShadowImageFilterGM : public skiagm::GM { |
| 73 | public: |
| 74 | DropShadowImageFilterGM () {} |
| 75 | |
| 76 | protected: |
| 77 | |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 78 | SkString onShortName() override { |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 79 | return SkString("dropshadowimagefilter"); |
| 80 | } |
| 81 | |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 82 | SkISize onISize() override { return SkISize::Make(400, 656); } |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 83 | |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 84 | void onDraw(SkCanvas* canvas) override { |
| 85 | void (*drawProc[])(SkCanvas*, const SkRect&, sk_sp<SkImageFilter>) = { |
reed | da420b9 | 2015-12-16 08:38:15 -0800 | [diff] [blame] | 86 | draw_bitmap, draw_path, draw_paint, draw_text |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 89 | sk_sp<SkColorFilter> cf(SkColorFilter::MakeModeFilter(SK_ColorMAGENTA, |
Mike Reed | 7d954ad | 2016-10-28 15:42:34 -0400 | [diff] [blame] | 90 | SkBlendMode::kSrcIn)); |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame] | 91 | sk_sp<SkImageFilter> cfif(SkColorFilterImageFilter::Make(std::move(cf), nullptr)); |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 92 | SkImageFilter::CropRect cropRect(SkRect::Make(SkIRect::MakeXYWH(10, 10, 44, 44)), |
| 93 | SkImageFilter::CropRect::kHasAll_CropEdge); |
| 94 | SkImageFilter::CropRect bogusRect(SkRect::Make(SkIRect::MakeXYWH(-100, -100, 10, 10)), |
| 95 | SkImageFilter::CropRect::kHasAll_CropEdge); |
| 96 | |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 97 | sk_sp<SkImageFilter> filters[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 98 | nullptr, |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 99 | SkDropShadowImageFilter::Make(7.0f, 0.0f, 0.0f, 3.0f, SK_ColorBLUE, |
| 100 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nullptr), |
| 101 | SkDropShadowImageFilter::Make(0.0f, 7.0f, 3.0f, 0.0f, SK_ColorBLUE, |
| 102 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nullptr), |
| 103 | SkDropShadowImageFilter::Make(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
| 104 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nullptr), |
| 105 | SkDropShadowImageFilter::Make(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
| 106 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, std::move(cfif)), |
| 107 | SkDropShadowImageFilter::Make(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 108 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nullptr, &cropRect), |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 109 | SkDropShadowImageFilter::Make(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 110 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nullptr, &bogusRect), |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 111 | SkDropShadowImageFilter::Make(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
| 112 | SkDropShadowImageFilter::kDrawShadowOnly_ShadowMode, nullptr), |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64)); |
| 116 | SkScalar MARGIN = SkIntToScalar(16); |
| 117 | SkScalar DX = r.width() + MARGIN; |
| 118 | SkScalar DY = r.height() + MARGIN; |
| 119 | |
| 120 | canvas->translate(MARGIN, MARGIN); |
| 121 | for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) { |
| 122 | canvas->save(); |
| 123 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 124 | drawProc[j](canvas, r, filters[i]); |
| 125 | canvas->translate(0, DY); |
| 126 | } |
| 127 | canvas->restore(); |
| 128 | canvas->translate(DX, 0); |
| 129 | } |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | private: |
| 133 | typedef GM INHERITED; |
| 134 | }; |
| 135 | |
| 136 | /////////////////////////////////////////////////////////////////////////////// |
| 137 | |
scroggo | 96f16e8 | 2015-12-10 13:31:59 -0800 | [diff] [blame] | 138 | DEF_GM( return new DropShadowImageFilterGM; ) |