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); |
Cary Clark | 992c7b0 | 2014-07-31 08:58:44 -0400 | [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); |
| 61 | draw_path(&c, r, NULL); |
| 62 | |
| 63 | paint.setImageFilter(imf); |
| 64 | canvas->save(); |
| 65 | canvas->clipRect(r); |
| 66 | canvas->drawBitmap(bm, 0, 0, &paint); |
| 67 | canvas->restore(); |
| 68 | } |
| 69 | |
| 70 | static void draw_sprite(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { |
| 71 | SkPaint paint; |
| 72 | |
| 73 | SkIRect bounds; |
| 74 | r.roundOut(&bounds); |
| 75 | |
| 76 | SkBitmap bm; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 77 | bm.allocN32Pixels(bounds.width(), bounds.height()); |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 78 | bm.eraseColor(SK_ColorRED); |
| 79 | SkCanvas c(bm); |
| 80 | |
| 81 | SkIRect cropRect = SkIRect::MakeXYWH(10, 10, 44, 44); |
| 82 | paint.setColor(SK_ColorGREEN); |
| 83 | c.drawRect(SkRect::Make(cropRect), paint); |
| 84 | |
| 85 | paint.setImageFilter(imf); |
| 86 | SkPoint loc = { r.fLeft, r.fTop }; |
| 87 | canvas->getTotalMatrix().mapPoints(&loc, 1); |
| 88 | canvas->drawSprite(bm, |
| 89 | SkScalarRoundToInt(loc.fX), SkScalarRoundToInt(loc.fY), |
| 90 | &paint); |
| 91 | } |
| 92 | |
| 93 | /////////////////////////////////////////////////////////////////////////////// |
| 94 | |
| 95 | class DropShadowImageFilterGM : public skiagm::GM { |
| 96 | public: |
| 97 | DropShadowImageFilterGM () {} |
| 98 | |
| 99 | protected: |
| 100 | |
| 101 | virtual SkString onShortName() { |
| 102 | return SkString("dropshadowimagefilter"); |
| 103 | } |
| 104 | |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame^] | 105 | virtual SkISize onISize() { return SkISize::Make(400, 656); } |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 106 | |
| 107 | void draw_frame(SkCanvas* canvas, const SkRect& r) { |
| 108 | SkPaint paint; |
| 109 | paint.setStyle(SkPaint::kStroke_Style); |
| 110 | paint.setColor(SK_ColorRED); |
| 111 | canvas->drawRect(r, paint); |
| 112 | } |
| 113 | |
| 114 | virtual uint32_t onGetFlags() const { |
| 115 | // Because of the use of drawSprite, this test is excluded |
| 116 | // from scaled replay tests because drawSprite ignores the |
| 117 | // reciprocal scale that is applied at record time, which is |
| 118 | // the intended behavior of drawSprite. |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 119 | return kSkipScaledReplay_Flag | kSkipTiled_Flag; |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | virtual void onDraw(SkCanvas* canvas) { |
| 123 | void (*drawProc[])(SkCanvas*, const SkRect&, SkImageFilter*) = { |
| 124 | draw_sprite, draw_bitmap, draw_path, draw_paint, draw_text |
| 125 | }; |
| 126 | |
| 127 | SkAutoTUnref<SkColorFilter> cf( |
| 128 | SkColorFilter::CreateModeFilter(SK_ColorMAGENTA, SkXfermode::kSrcIn_Mode)); |
| 129 | SkAutoTUnref<SkImageFilter> cfif(SkColorFilterImageFilter::Create(cf.get())); |
| 130 | SkImageFilter::CropRect cropRect(SkRect::Make(SkIRect::MakeXYWH(10, 10, 44, 44)), |
| 131 | SkImageFilter::CropRect::kHasAll_CropEdge); |
| 132 | SkImageFilter::CropRect bogusRect(SkRect::Make(SkIRect::MakeXYWH(-100, -100, 10, 10)), |
| 133 | SkImageFilter::CropRect::kHasAll_CropEdge); |
| 134 | |
| 135 | SkImageFilter* filters[] = { |
| 136 | NULL, |
sugoi | 234f036 | 2014-10-23 13:59:52 -0700 | [diff] [blame^] | 137 | SkDropShadowImageFilter::Create(7.0f, 0.0f, 0.0f, 3.0f, SK_ColorBLUE, |
| 138 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode), |
| 139 | SkDropShadowImageFilter::Create(0.0f, 7.0f, 3.0f, 0.0f, SK_ColorBLUE, |
| 140 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode), |
| 141 | SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
| 142 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode), |
| 143 | SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
| 144 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, cfif, NULL, 0), |
| 145 | SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
| 146 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, NULL, &cropRect, 0), |
| 147 | SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
| 148 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, NULL, &bogusRect, 0), |
| 149 | SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, |
| 150 | SkDropShadowImageFilter::kDrawShadowOnly_ShadowMode), |
senorblanco@chromium.org | d8f82a4 | 2013-11-26 21:52:38 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64)); |
| 154 | SkScalar MARGIN = SkIntToScalar(16); |
| 155 | SkScalar DX = r.width() + MARGIN; |
| 156 | SkScalar DY = r.height() + MARGIN; |
| 157 | |
| 158 | canvas->translate(MARGIN, MARGIN); |
| 159 | for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) { |
| 160 | canvas->save(); |
| 161 | for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) { |
| 162 | drawProc[j](canvas, r, filters[i]); |
| 163 | canvas->translate(0, DY); |
| 164 | } |
| 165 | canvas->restore(); |
| 166 | canvas->translate(DX, 0); |
| 167 | } |
| 168 | |
| 169 | for(size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) { |
| 170 | SkSafeUnref(filters[j]); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | private: |
| 175 | typedef GM INHERITED; |
| 176 | }; |
| 177 | |
| 178 | /////////////////////////////////////////////////////////////////////////////// |
| 179 | |
| 180 | static skiagm::GM* MyFactory(void*) { return new DropShadowImageFilterGM; } |
| 181 | static skiagm::GMRegistry reg(MyFactory); |