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