senorblanco | 2aec4e9 | 2015-08-31 10:28:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
senorblanco | 2aec4e9 | 2015-08-31 10:28:49 -0700 | [diff] [blame] | 8 | #include "SkBlurImageFilter.h" |
| 9 | #include "SkColor.h" |
| 10 | #include "SkDropShadowImageFilter.h" |
| 11 | #include "SkOffsetImageFilter.h" |
| 12 | #include "SkScalar.h" |
| 13 | #include "gm.h" |
| 14 | |
| 15 | #define RESIZE_FACTOR_X SkIntToScalar(2) |
| 16 | #define RESIZE_FACTOR_Y SkIntToScalar(5) |
| 17 | |
| 18 | namespace skiagm { |
| 19 | |
| 20 | class ImageFiltersStrokedGM : public GM { |
| 21 | public: |
| 22 | ImageFiltersStrokedGM() { |
| 23 | this->setBGColor(0x00000000); |
| 24 | } |
| 25 | |
| 26 | protected: |
| 27 | |
| 28 | SkString onShortName() override { |
| 29 | return SkString("imagefiltersstroked"); |
| 30 | } |
| 31 | |
| 32 | SkISize onISize() override { |
| 33 | return SkISize::Make(860, 500); |
| 34 | } |
| 35 | |
| 36 | static void draw_circle(SkCanvas* canvas, const SkRect& r, const SkPaint& paint) { |
| 37 | canvas->drawCircle(r.centerX(), r.centerY(), |
| 38 | r.width() * 2 / 5, paint); |
| 39 | } |
| 40 | |
| 41 | static void draw_line(SkCanvas* canvas, const SkRect& r, const SkPaint& paint) { |
| 42 | canvas->drawLine(r.fLeft, r.fBottom, r.fRight, r.fTop, paint); |
| 43 | } |
| 44 | |
| 45 | static void draw_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& paint) { |
| 46 | canvas->drawRect(r, paint); |
| 47 | } |
| 48 | |
| 49 | void onDraw(SkCanvas* canvas) override { |
| 50 | void (*drawProc[])(SkCanvas*, const SkRect&, const SkPaint&) = { |
| 51 | draw_line, draw_rect, draw_circle, |
| 52 | }; |
| 53 | |
| 54 | canvas->clear(SK_ColorBLACK); |
| 55 | |
| 56 | SkMatrix resizeMatrix; |
| 57 | resizeMatrix.setScale(RESIZE_FACTOR_X, RESIZE_FACTOR_Y); |
| 58 | |
robertphillips | ae8c933 | 2016-04-05 15:09:00 -0700 | [diff] [blame] | 59 | sk_sp<SkImageFilter> filters[] = { |
| 60 | SkBlurImageFilter::Make(5, 5, nullptr), |
robertphillips | c416912 | 2016-04-06 08:40:59 -0700 | [diff] [blame] | 61 | SkDropShadowImageFilter::Make(10, 10, 3, 3, SK_ColorGREEN, |
| 62 | SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, |
| 63 | nullptr), |
robertphillips | ae8c933 | 2016-04-05 15:09:00 -0700 | [diff] [blame] | 64 | SkOffsetImageFilter::Make(-16, 32, nullptr), |
| 65 | SkImageFilter::MakeMatrixFilter(resizeMatrix, kNone_SkFilterQuality, nullptr), |
senorblanco | 2aec4e9 | 2015-08-31 10:28:49 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | SkRect r = SkRect::MakeWH(64, 64); |
| 69 | SkScalar margin = 32; |
| 70 | SkPaint paint; |
| 71 | paint.setColor(SK_ColorWHITE); |
| 72 | paint.setAntiAlias(true); |
| 73 | paint.setStrokeWidth(10); |
| 74 | paint.setStyle(SkPaint::kStroke_Style); |
| 75 | paint.setTextSize(48); |
| 76 | paint.setTextAlign(SkPaint::kCenter_Align); |
| 77 | |
| 78 | for (size_t i = 0; i < SK_ARRAY_COUNT(drawProc); ++i) { |
| 79 | canvas->translate(0, margin); |
| 80 | canvas->save(); |
| 81 | for (size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) { |
| 82 | canvas->translate(margin, 0); |
| 83 | canvas->save(); |
| 84 | if (2 == j) { |
| 85 | canvas->translate(16, -32); |
| 86 | } else if (3 == j) { |
| 87 | canvas->scale(SkScalarInvert(RESIZE_FACTOR_X), |
| 88 | SkScalarInvert(RESIZE_FACTOR_Y)); |
| 89 | } |
| 90 | paint.setImageFilter(filters[j]); |
| 91 | drawProc[i](canvas, r, paint); |
| 92 | canvas->restore(); |
| 93 | canvas->translate(r.width() + margin, 0); |
| 94 | } |
| 95 | canvas->restore(); |
| 96 | canvas->translate(0, r.height()); |
| 97 | } |
senorblanco | 2aec4e9 | 2015-08-31 10:28:49 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | private: |
| 101 | typedef GM INHERITED; |
| 102 | }; |
| 103 | |
| 104 | ////////////////////////////////////////////////////////////////////////////// |
| 105 | |
| 106 | DEF_GM(return new ImageFiltersStrokedGM;) |
| 107 | |
| 108 | } |