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" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkColorFilter.h" |
| 11 | #include "SkColorPriv.h" |
| 12 | #include "SkShader.h" |
| 13 | |
| 14 | #include "SkBlurImageFilter.h" |
senorblanco@chromium.org | 44888c6 | 2012-08-20 19:23:24 +0000 | [diff] [blame] | 15 | #include "SkColorFilterImageFilter.h" |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 16 | #include "SkDropShadowImageFilter.h" |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 17 | #include "SkTestImageFilters.h" |
| 18 | |
| 19 | class FailImageFilter : public SkImageFilter { |
| 20 | public: |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 21 | static FailImageFilter* Create() { |
| 22 | return SkNEW(FailImageFilter); |
| 23 | } |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 24 | |
| 25 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(FailImageFilter) |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 26 | protected: |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 27 | FailImageFilter() : INHERITED(0) {} |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 28 | virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 29 | SkBitmap* result, SkIPoint* offset) const SK_OVERRIDE { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 30 | return false; |
| 31 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 32 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 33 | FailImageFilter(SkReadBuffer& buffer) |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 34 | : INHERITED(1, buffer) {} |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 35 | |
| 36 | private: |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 37 | typedef SkImageFilter INHERITED; |
| 38 | }; |
| 39 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 40 | // register the filter with the flattenable registry |
| 41 | static SkFlattenable::Registrar gFailImageFilterReg("FailImageFilter", |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 42 | FailImageFilter::CreateProc, |
| 43 | FailImageFilter::GetFlattenableType()); |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 44 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 45 | class IdentityImageFilter : public SkImageFilter { |
| 46 | public: |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 47 | static IdentityImageFilter* Create() { |
| 48 | return SkNEW(IdentityImageFilter); |
| 49 | } |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 50 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 51 | SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(IdentityImageFilter) |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 52 | protected: |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 53 | IdentityImageFilter() : INHERITED(0) {} |
senorblanco@chromium.org | 4cb543d | 2014-03-14 15:44:01 +0000 | [diff] [blame] | 54 | virtual bool onFilterImage(Proxy*, const SkBitmap& src, const Context&, |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 55 | SkBitmap* result, SkIPoint* offset) const SK_OVERRIDE { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 56 | *result = src; |
commit-bot@chromium.org | f7efa50 | 2014-04-11 18:57:00 +0000 | [diff] [blame] | 57 | offset->set(0, 0); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 58 | return true; |
| 59 | } |
| 60 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 61 | IdentityImageFilter(SkReadBuffer& buffer) |
commit-bot@chromium.org | ce33d60 | 2013-11-25 21:46:31 +0000 | [diff] [blame] | 62 | : INHERITED(1, buffer) {} |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 63 | |
| 64 | private: |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 65 | typedef SkImageFilter INHERITED; |
| 66 | }; |
| 67 | |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 68 | // register the filter with the flattenable registry |
| 69 | static SkFlattenable::Registrar gIdentityImageFilterReg("IdentityImageFilter", |
commit-bot@chromium.org | c0b7e10 | 2013-10-23 17:06:21 +0000 | [diff] [blame] | 70 | IdentityImageFilter::CreateProc, |
| 71 | IdentityImageFilter::GetFlattenableType()); |
djsollen@google.com | ba28d03 | 2012-03-26 17:57:35 +0000 | [diff] [blame] | 72 | |
| 73 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 74 | /////////////////////////////////////////////////////////////////////////////// |
| 75 | |
| 76 | static void draw_paint(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { |
| 77 | SkPaint paint; |
| 78 | paint.setImageFilter(imf); |
| 79 | paint.setColor(SK_ColorGREEN); |
| 80 | canvas->save(); |
| 81 | canvas->clipRect(r); |
| 82 | canvas->drawPaint(paint); |
| 83 | canvas->restore(); |
| 84 | } |
| 85 | |
| 86 | static void draw_line(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { |
| 87 | SkPaint paint; |
| 88 | paint.setColor(SK_ColorBLUE); |
| 89 | paint.setImageFilter(imf); |
| 90 | paint.setStrokeWidth(r.width()/10); |
| 91 | canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, paint); |
| 92 | } |
| 93 | |
| 94 | static void draw_rect(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { |
| 95 | SkPaint paint; |
| 96 | paint.setColor(SK_ColorYELLOW); |
| 97 | paint.setImageFilter(imf); |
| 98 | SkRect rr(r); |
| 99 | rr.inset(r.width()/10, r.height()/10); |
| 100 | canvas->drawRect(rr, paint); |
| 101 | } |
| 102 | |
| 103 | static void draw_path(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { |
| 104 | SkPaint paint; |
| 105 | paint.setColor(SK_ColorMAGENTA); |
| 106 | paint.setImageFilter(imf); |
| 107 | paint.setAntiAlias(true); |
| 108 | canvas->drawCircle(r.centerX(), r.centerY(), r.width()*2/5, paint); |
| 109 | } |
| 110 | |
| 111 | static void draw_text(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { |
| 112 | SkPaint paint; |
| 113 | paint.setImageFilter(imf); |
| 114 | paint.setColor(SK_ColorCYAN); |
| 115 | paint.setAntiAlias(true); |
| 116 | paint.setTextSize(r.height()/2); |
| 117 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 118 | canvas->drawText("Text", 4, r.centerX(), r.centerY(), paint); |
| 119 | } |
| 120 | |
| 121 | static void draw_bitmap(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { |
| 122 | SkPaint paint; |
| 123 | paint.setImageFilter(imf); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 124 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 125 | SkIRect bounds; |
| 126 | r.roundOut(&bounds); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 127 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 128 | SkBitmap bm; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 129 | bm.allocN32Pixels(bounds.width(), bounds.height()); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 130 | bm.eraseColor(SK_ColorTRANSPARENT); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 131 | SkCanvas c(bm); |
| 132 | draw_path(&c, r, NULL); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 133 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 134 | canvas->drawBitmap(bm, 0, 0, &paint); |
| 135 | } |
| 136 | |
| 137 | static void draw_sprite(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) { |
| 138 | SkPaint paint; |
| 139 | paint.setImageFilter(imf); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 140 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 141 | SkIRect bounds; |
| 142 | r.roundOut(&bounds); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 143 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 144 | SkBitmap bm; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 145 | bm.allocN32Pixels(bounds.width(), bounds.height()); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 146 | bm.eraseColor(SK_ColorTRANSPARENT); |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 147 | SkCanvas c(bm); |
| 148 | draw_path(&c, r, NULL); |
| 149 | |
| 150 | SkPoint loc = { r.fLeft, r.fTop }; |
| 151 | canvas->getTotalMatrix().mapPoints(&loc, 1); |
| 152 | canvas->drawSprite(bm, |
| 153 | SkScalarRoundToInt(loc.fX), SkScalarRoundToInt(loc.fY), |
| 154 | &paint); |
| 155 | } |
| 156 | |
| 157 | /////////////////////////////////////////////////////////////////////////////// |
| 158 | |
| 159 | class ImageFiltersBaseGM : public skiagm::GM { |
| 160 | public: |
| 161 | ImageFiltersBaseGM () {} |
| 162 | |
| 163 | protected: |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 164 | virtual SkString onShortName() { |
| 165 | return SkString("imagefiltersbase"); |
| 166 | } |
| 167 | |
junov@chromium.org | f44fcdc | 2013-08-07 20:00:55 +0000 | [diff] [blame] | 168 | virtual SkISize onISize() { return SkISize::Make(700, 500); } |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 169 | |
| 170 | void draw_frame(SkCanvas* canvas, const SkRect& r) { |
| 171 | SkPaint paint; |
| 172 | paint.setStyle(SkPaint::kStroke_Style); |
| 173 | paint.setColor(SK_ColorRED); |
| 174 | canvas->drawRect(r, paint); |
| 175 | } |
| 176 | |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 177 | virtual uint32_t onGetFlags() const { |
| 178 | // Because of the use of drawSprite, this test is excluded |
| 179 | // from scaled replay tests because drawSprite ignores the |
| 180 | // reciprocal scale that is applied at record time, which is |
| 181 | // the intended behavior of drawSprite. |
commit-bot@chromium.org | a90c680 | 2014-04-30 13:20:45 +0000 | [diff] [blame] | 182 | return kSkipScaledReplay_Flag | kSkipTiled_Flag; |
junov@chromium.org | ff06af2 | 2013-01-14 16:27:50 +0000 | [diff] [blame] | 183 | } |
| 184 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 185 | virtual void onDraw(SkCanvas* canvas) { |
| 186 | void (*drawProc[])(SkCanvas*, const SkRect&, SkImageFilter*) = { |
| 187 | draw_paint, |
| 188 | draw_line, draw_rect, draw_path, draw_text, |
| 189 | draw_bitmap, |
| 190 | draw_sprite |
| 191 | }; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 192 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 193 | SkColorFilter* cf = SkColorFilter::CreateModeFilter(SK_ColorRED, |
| 194 | SkXfermode::kSrcIn_Mode); |
| 195 | SkImageFilter* filters[] = { |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 196 | NULL, |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 197 | IdentityImageFilter::Create(), |
| 198 | FailImageFilter::Create(), |
senorblanco@chromium.org | cd9f559 | 2012-10-26 19:37:00 +0000 | [diff] [blame] | 199 | SkColorFilterImageFilter::Create(cf), |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 200 | SkBlurImageFilter::Create(12.0f, 0.0f), |
| 201 | SkDropShadowImageFilter::Create(10.0f, 5.0f, 3.0f, SK_ColorBLUE), |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 202 | }; |
| 203 | cf->unref(); |
| 204 | |
| 205 | SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64)); |
| 206 | SkScalar MARGIN = SkIntToScalar(16); |
| 207 | SkScalar DX = r.width() + MARGIN; |
| 208 | SkScalar DY = r.height() + MARGIN; |
| 209 | |
| 210 | canvas->translate(MARGIN, MARGIN); |
| 211 | for (size_t i = 0; i < SK_ARRAY_COUNT(drawProc); ++i) { |
| 212 | canvas->save(); |
| 213 | for (size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) { |
| 214 | drawProc[i](canvas, r, filters[j]); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 215 | |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 216 | draw_frame(canvas, r); |
| 217 | canvas->translate(0, DY); |
| 218 | } |
| 219 | canvas->restore(); |
| 220 | canvas->translate(DX, 0); |
| 221 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 222 | |
reed@google.com | 4bca84d | 2012-06-27 14:00:06 +0000 | [diff] [blame] | 223 | for(size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) { |
| 224 | SkSafeUnref(filters[j]); |
| 225 | } |
reed@google.com | 8926b16 | 2012-03-23 15:36:36 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | private: |
| 229 | typedef GM INHERITED; |
| 230 | }; |
| 231 | |
| 232 | /////////////////////////////////////////////////////////////////////////////// |
| 233 | |
| 234 | static skiagm::GM* MyFactory(void*) { return new ImageFiltersBaseGM; } |
| 235 | static skiagm::GMRegistry reg(MyFactory); |