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/SkCanvas.h" 10 #include "include/core/SkPaint.h" 11 #include "include/core/SkRect.h" 12 #include "include/core/SkScalar.h" 13 #include "include/core/SkSize.h" 14 #include "include/core/SkString.h" 15 #include "src/base/SkRandom.h" 16 17 namespace skiagm { 18 19 #define W 400 20 #define H 400 21 #define N 100 22 23 constexpr SkScalar SW = SkIntToScalar(W); 24 constexpr SkScalar SH = SkIntToScalar(H); 25 26 class StrokeRectsGM : public GM { 27 public: StrokeRectsGM(bool rotated)28 StrokeRectsGM(bool rotated) : fRotated(rotated) {} 29 30 protected: getName() const31 SkString getName() const override { 32 if (fRotated) { 33 return SkString("strokerects_rotated"); 34 } else { 35 return SkString("strokerects"); 36 } 37 } 38 getISize()39 SkISize getISize() override { return SkISize::Make(W * 2, H * 2); } 40 rnd_rect(SkRect * r,SkRandom & rand)41 static void rnd_rect(SkRect* r, SkRandom& rand) { 42 SkScalar x = rand.nextUScalar1() * W; 43 SkScalar y = rand.nextUScalar1() * H; 44 SkScalar w = rand.nextUScalar1() * (W >> 2); 45 SkScalar h = rand.nextUScalar1() * (H >> 2); 46 SkScalar hoffset = rand.nextSScalar1(); 47 SkScalar woffset = rand.nextSScalar1(); 48 49 r->setXYWH(x, y, w, h); 50 r->offset(-w/2 + woffset, -h/2 + hoffset); 51 } 52 onDraw(SkCanvas * canvas)53 void onDraw(SkCanvas* canvas) override { 54 if (fRotated) { 55 canvas->rotate(45.f, SW, SH); 56 } 57 58 SkPaint paint; 59 paint.setStyle(SkPaint::kStroke_Style); 60 61 for (int y = 0; y < 2; y++) { 62 paint.setAntiAlias(!!y); 63 for (int x = 0; x < 2; x++) { 64 paint.setStrokeWidth(x * SkIntToScalar(3)); 65 66 SkAutoCanvasRestore acr(canvas, true); 67 canvas->translate(SW * x, SH * y); 68 canvas->clipRect(SkRect::MakeLTRB( 69 SkIntToScalar(2), SkIntToScalar(2) 70 , SW - SkIntToScalar(2), SH - SkIntToScalar(2) 71 )); 72 73 SkRandom rand; 74 for (int i = 0; i < N; i++) { 75 SkRect r; 76 rnd_rect(&r, rand); 77 canvas->drawRect(r, paint); 78 } 79 } 80 } 81 } 82 83 private: 84 bool fRotated; 85 }; 86 87 ////////////////////////////////////////////////////////////////////////////// 88 89 DEF_GM( return new StrokeRectsGM(false); ) 90 DEF_GM( return new StrokeRectsGM(true); ) 91 92 } // namespace skiagm 93