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/gm.h" 9 #include "include/core/SkBlurTypes.h" 10 #include "include/core/SkCanvas.h" 11 #include "include/core/SkColor.h" 12 #include "include/core/SkPaint.h" 13 #include "include/core/SkPoint.h" 14 #include "include/core/SkScalar.h" 15 #include "include/core/SkSize.h" 16 #include "include/core/SkString.h" 17 #include "src/base/SkRandom.h" 18 19 #include <stddef.h> 20 21 namespace skiagm { 22 23 class PointsGM : public GM { 24 public: PointsGM()25 PointsGM() {} 26 27 protected: getName() const28 SkString getName() const override { return SkString("points"); } 29 getISize()30 SkISize getISize() override { return SkISize::Make(640, 490); } 31 fill_pts(SkPoint pts[],size_t n,SkRandom * rand)32 static void fill_pts(SkPoint pts[], size_t n, SkRandom* rand) { 33 for (size_t i = 0; i < n; i++) { 34 // Compute these independently and store in variables, rather 35 // than in the parameter-passing expression, to get consistent 36 // evaluation order across compilers. 37 SkScalar y = rand->nextUScalar1() * 480; 38 SkScalar x = rand->nextUScalar1() * 640; 39 pts[i].set(x, y); 40 } 41 } 42 onDraw(SkCanvas * canvas)43 void onDraw(SkCanvas* canvas) override { 44 canvas->translate(SK_Scalar1, SK_Scalar1); 45 46 SkRandom rand; 47 SkPaint p0, p1, p2, p3; 48 const size_t n = 99; 49 50 p0.setColor(SK_ColorRED); 51 p1.setColor(SK_ColorGREEN); 52 p2.setColor(SK_ColorBLUE); 53 p3.setColor(SK_ColorWHITE); 54 55 p0.setStrokeWidth(SkIntToScalar(4)); 56 p2.setStrokeCap(SkPaint::kRound_Cap); 57 p2.setStrokeWidth(SkIntToScalar(6)); 58 59 SkPoint* pts = new SkPoint[n]; 60 fill_pts(pts, n, &rand); 61 62 canvas->drawPoints(SkCanvas::kPolygon_PointMode, n, pts, p0); 63 canvas->drawPoints(SkCanvas::kLines_PointMode, n, pts, p1); 64 canvas->drawPoints(SkCanvas::kPoints_PointMode, n, pts, p2); 65 canvas->drawPoints(SkCanvas::kPoints_PointMode, n, pts, p3); 66 67 delete[] pts; 68 } 69 70 private: 71 using INHERITED = GM; 72 }; 73 DEF_GM( return new PointsGM; ) 74 } // namespace skiagm 75 76 #include "include/core/SkMaskFilter.h" 77 78 DEF_SIMPLE_GM(points_maskfilter, canvas, 512, 256) { 79 constexpr int N = 30; 80 SkPoint pts[N]; 81 82 SkRandom rand; 83 for (SkPoint& p : pts) { 84 p.fX = rand.nextF() * 220 + 18; 85 p.fY = rand.nextF() * 220 + 18; 86 } 87 88 auto mf = SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 6); 89 const SkPaint::Cap caps[] = { SkPaint::kSquare_Cap, SkPaint::kRound_Cap }; 90 91 SkPaint paint; 92 paint.setAntiAlias(true); 93 paint.setStroke(true); 94 paint.setStrokeWidth(10); 95 96 for (auto cap : caps) { 97 paint.setStrokeCap(cap); 98 99 paint.setMaskFilter(mf); 100 paint.setColor(SK_ColorBLACK); 101 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, pts, paint); 102 103 paint.setMaskFilter(nullptr); 104 paint.setColor(SK_ColorRED); 105 canvas->drawPoints(SkCanvas::kPoints_PointMode, N, pts, paint); 106 107 canvas->translate(256, 0); 108 } 109 } 110