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.h" 9 10 #include "SkDashPathEffect.h" 11 #include "SkPaint.h" 12 #include "SkPath.h" 13 #include "SkRRect.h" 14 15 namespace skiagm { 16 17 class ContourStartGM : public GM { 18 public: ContourStartGM()19 ContourStartGM() { 20 const SkScalar kMaxDashLen = 100; 21 const SkScalar kDashGrowth = 1.2f; 22 23 SkSTArray<100, SkScalar> intervals; 24 for (SkScalar len = 1; len < kMaxDashLen; len *= kDashGrowth) { 25 intervals.push_back(len); 26 intervals.push_back(len); 27 } 28 29 SkAutoTUnref<SkPathEffect> effect( 30 SkDashPathEffect::Create(intervals.begin(), intervals.count(), 0)); 31 32 fDashPaint.setAntiAlias(true); 33 fDashPaint.setStyle(SkPaint::kStroke_Style); 34 fDashPaint.setStrokeWidth(6); 35 fDashPaint.setColor(0xff008000); 36 fDashPaint.setPathEffect(effect); 37 38 fPointsPaint.setColor(0xff800000); 39 fPointsPaint.setStrokeWidth(3); 40 41 fRect = SkRect::MakeLTRB(10, 10, 100, 70); 42 } 43 44 protected: onShortName()45 SkString onShortName() override { 46 return SkString("contour_start"); 47 } 48 onISize()49 SkISize onISize() override { return SkISize::Make(kImageWidth, kImageHeight); } 50 onDraw(SkCanvas * canvas)51 void onDraw(SkCanvas* canvas) override { 52 53 drawDirs(canvas, [](const SkRect& rect, SkPath::Direction dir, unsigned startIndex) { 54 SkPath path; 55 path.addRect(rect, dir, startIndex); 56 return path; 57 }); 58 59 drawDirs(canvas, [](const SkRect& rect, SkPath::Direction dir, unsigned startIndex) { 60 SkPath path; 61 path.addOval(rect, dir, startIndex); 62 return path; 63 }); 64 65 drawDirs(canvas, [](const SkRect& rect, SkPath::Direction dir, unsigned startIndex) { 66 SkRRect rrect; 67 const SkVector radii[4] = { {15, 15}, {15, 15}, {15, 15}, {15, 15}}; 68 rrect.setRectRadii(rect, radii); 69 70 SkPath path; 71 path.addRRect(rrect, dir, startIndex); 72 return path; 73 }); 74 75 drawDirs(canvas, [](const SkRect& rect, SkPath::Direction dir, unsigned startIndex) { 76 SkRRect rrect; 77 rrect.setRect(rect); 78 79 SkPath path; 80 path.addRRect(rrect, dir, startIndex); 81 return path; 82 }); 83 84 drawDirs(canvas, [](const SkRect& rect, SkPath::Direction dir, unsigned startIndex) { 85 SkRRect rrect; 86 rrect.setOval(rect); 87 88 SkPath path; 89 path.addRRect(rrect, dir, startIndex); 90 return path; 91 }); 92 93 } 94 95 private: 96 static const int kImageWidth = 1200; 97 static const int kImageHeight = 600; 98 99 SkPaint fDashPaint, fPointsPaint; 100 SkRect fRect; 101 drawDirs(SkCanvas * canvas,SkPath (* makePath)(const SkRect &,SkPath::Direction,unsigned)) const102 void drawDirs(SkCanvas* canvas, 103 SkPath (*makePath)(const SkRect&, SkPath::Direction, unsigned)) const { 104 drawOneColumn(canvas, SkPath::kCW_Direction, makePath); 105 canvas->translate(kImageWidth / 10, 0); 106 drawOneColumn(canvas, SkPath::kCCW_Direction, makePath); 107 canvas->translate(kImageWidth / 10, 0); 108 } 109 drawOneColumn(SkCanvas * canvas,SkPath::Direction dir,SkPath (* makePath)(const SkRect &,SkPath::Direction,unsigned)) const110 void drawOneColumn(SkCanvas* canvas, SkPath::Direction dir, 111 SkPath (*makePath)(const SkRect&, SkPath::Direction, unsigned)) const { 112 SkAutoCanvasRestore acr(canvas, true); 113 114 for (unsigned i = 0; i < 8; ++i) { 115 const SkPath path = makePath(fRect, dir, i); 116 canvas->drawPath(path, fDashPaint); 117 118 const int n = path.countPoints(); 119 SkAutoTArray<SkPoint> points(n); 120 path.getPoints(points.get(), n); 121 canvas->drawPoints(SkCanvas::kPoints_PointMode, n, points.get(), fPointsPaint); 122 123 canvas->translate(0, kImageHeight / 8); 124 } 125 } 126 127 typedef GM INHERITED; 128 }; 129 130 DEF_GM( return new ContourStartGM(); ) 131 132 } // namespace skiagm 133 134