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 | |
| 16 | static void draw_paint(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { |
| 17 | SkPaint paint; |
| 18 | paint.setImageFilter(imf); |
| 19 | paint.setColor(SK_ColorBLACK); |
| 20 | canvas->save(); |
| 21 | canvas->clipRect(r); |
| 22 | canvas->drawPaint(paint); |
| 23 | canvas->restore(); |
| 24 | } |
| 25 | |
| 26 | static void draw_path(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { |
| 27 | SkPaint paint; |
| 28 | paint.setColor(SK_ColorGREEN); |
| 29 | paint.setImageFilter(imf); |
| 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 | |
| 37 | static void draw_text(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { |
| 38 | SkPaint paint; |
| 39 | paint.setImageFilter(imf); |
| 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 | |
| 51 | static void draw_bitmap(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { |
| 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 | |
| 63 | paint.setImageFilter(imf); |
| 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 | |
| 78 | virtual SkString onShortName() { |
| 79 | return SkString("dropshadowimagefilter"); |
| 80 | } |
| 81 | |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame] | 82 | virtual SkISize onISize() { return SkISize::Make(400, 656); } |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 83 | |
| 84 | void draw_frame(SkCanvas* canvas, const SkRect& r) { |
| 85 | SkPaint paint; |
| 86 | paint.setStyle(SkPaint::kStroke_Style); |
| 87 | paint.setColor(SK_ColorRED); |
| 88 | canvas->drawRect(r, paint); |
| 89 | } |
| 90 | |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 91 | virtual void onDraw(SkCanvas* canvas) { |
| 92 | void (*drawProc[])(SkCanvas*, const SkRect&, SkImageFilter*) = { |
reed | da420b9 | 2015-12-16 08:38:15 -0800 | [diff] [blame] | 93 | draw_bitmap, draw_path, draw_paint, draw_text |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame^] | 96 | sk_sp<SkColorFilter> cf(SkColorFilter::MakeModeFilter(SK_ColorMAGENTA, |
| 97 | SkXfermode::kSrcIn_Mode)); |
| 98 | sk_sp<SkImageFilter> cfif(SkColorFilterImageFilter::Make(std::move(cf), nullptr)); |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 99 | SkImageFilter::CropRect cropRect(SkRect::Make(SkIRect::MakeXYWH(10, 10, 44, 44)), |
| 100 | SkImageFilter::CropRect::kHasAll_CropEdge); |
| 101 | SkImageFilter::CropRect bogusRect(SkRect::Make(SkIRect::MakeXYWH(-100, -100, 10, 10)), |
| 102 | SkImageFilter::CropRect::kHasAll_CropEdge); |
| 103 | |
| 104 | SkImageFilter* filters[] = { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 105 | nullptr, |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame] | 106 | SkDropShadowImageFilter::Create(7.0f, 0.0f, 0.0f, 3.0f, SK_ColorBLUE, |
| 107 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode), |
| 108 | SkDropShadowImageFilter::Create(0.0f, 7.0f, 3.0f, 0.0f, SK_ColorBLUE, |
| 109 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode), |
| 110 | SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
| 111 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode), |
| 112 | SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
robertphillips | 5605b56 | 2016-04-05 11:50:42 -0700 | [diff] [blame^] | 113 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, cfif.get(), nullptr), |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame] | 114 | SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 115 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nullptr, &cropRect), |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame] | 116 | SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 117 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, nullptr, &bogusRect), |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame] | 118 | SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
| 119 | SkDropShadowImageFilter::kDrawShadowOnly_ShadowMode), |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64)); |
| 123 | SkScalar MARGIN = SkIntToScalar(16); |
| 124 | SkScalar DX = r.width() + MARGIN; |
| 125 | SkScalar DY = r.height() + MARGIN; |
| 126 | |
| 127 | canvas->translate(MARGIN, MARGIN); |
| 128 | for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) { |
| 129 | canvas->save(); |
| 130 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 131 | drawProc[j](canvas, r, filters[i]); |
| 132 | canvas->translate(0, DY); |
| 133 | } |
| 134 | canvas->restore(); |
| 135 | canvas->translate(DX, 0); |
| 136 | } |
| 137 | |
| 138 | for(size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) { |
| 139 | SkSafeUnref(filters[j]); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | private: |
| 144 | typedef GM INHERITED; |
| 145 | }; |
| 146 | |
| 147 | /////////////////////////////////////////////////////////////////////////////// |
| 148 | |
scroggo | 96f16e8 | 2015-12-10 13:31:59 -0800 | [diff] [blame] | 149 | DEF_GM( return new DropShadowImageFilterGM; ) |