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 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: ImageFiltersStrokedGM()22 ImageFiltersStrokedGM() { 23 this->setBGColor(0x00000000); 24 } 25 26 protected: 27 onShortName()28 SkString onShortName() override { 29 return SkString("imagefiltersstroked"); 30 } 31 onISize()32 SkISize onISize() override { 33 return SkISize::Make(860, 500); 34 } 35 draw_circle(SkCanvas * canvas,const SkRect & r,const SkPaint & paint)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 draw_line(SkCanvas * canvas,const SkRect & r,const SkPaint & paint)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 draw_rect(SkCanvas * canvas,const SkRect & r,const SkPaint & paint)45 static void draw_rect(SkCanvas* canvas, const SkRect& r, const SkPaint& paint) { 46 canvas->drawRect(r, paint); 47 } 48 onDraw(SkCanvas * canvas)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 59 sk_sp<SkImageFilter> filters[] = { 60 SkBlurImageFilter::Make(5, 5, nullptr), 61 SkDropShadowImageFilter::Make(10, 10, 3, 3, SK_ColorGREEN, 62 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, 63 nullptr), 64 SkOffsetImageFilter::Make(-16, 32, nullptr), 65 SkImageFilter::MakeMatrixFilter(resizeMatrix, kNone_SkFilterQuality, nullptr), 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 76 for (size_t i = 0; i < SK_ARRAY_COUNT(drawProc); ++i) { 77 canvas->translate(0, margin); 78 canvas->save(); 79 for (size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) { 80 canvas->translate(margin, 0); 81 canvas->save(); 82 if (2 == j) { 83 canvas->translate(16, -32); 84 } else if (3 == j) { 85 canvas->scale(SkScalarInvert(RESIZE_FACTOR_X), 86 SkScalarInvert(RESIZE_FACTOR_Y)); 87 } 88 paint.setImageFilter(filters[j]); 89 drawProc[i](canvas, r, paint); 90 canvas->restore(); 91 canvas->translate(r.width() + margin, 0); 92 } 93 canvas->restore(); 94 canvas->translate(0, r.height()); 95 } 96 } 97 98 private: 99 typedef GM INHERITED; 100 }; 101 102 ////////////////////////////////////////////////////////////////////////////// 103 104 DEF_GM(return new ImageFiltersStrokedGM;) 105 106 } 107