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 "include/utils/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()28 StrokeRectsGM() {} 29 30 protected: 31 onShortName()32 SkString onShortName() override { 33 return SkString("strokerects"); 34 } 35 onISize()36 SkISize onISize() override { 37 return SkISize::Make(W*2, H*2); 38 } 39 rnd_rect(SkRect * r,SkRandom & rand)40 static void rnd_rect(SkRect* r, SkRandom& rand) { 41 SkScalar x = rand.nextUScalar1() * W; 42 SkScalar y = rand.nextUScalar1() * H; 43 SkScalar w = rand.nextUScalar1() * (W >> 2); 44 SkScalar h = rand.nextUScalar1() * (H >> 2); 45 SkScalar hoffset = rand.nextSScalar1(); 46 SkScalar woffset = rand.nextSScalar1(); 47 48 r->set(x, y, x + w, y + h); 49 r->offset(-w/2 + woffset, -h/2 + hoffset); 50 } 51 onDraw(SkCanvas * canvas)52 void onDraw(SkCanvas* canvas) override { 53 SkPaint paint; 54 paint.setStyle(SkPaint::kStroke_Style); 55 56 for (int y = 0; y < 2; y++) { 57 paint.setAntiAlias(!!y); 58 for (int x = 0; x < 2; x++) { 59 paint.setStrokeWidth(x * SkIntToScalar(3)); 60 61 SkAutoCanvasRestore acr(canvas, true); 62 canvas->translate(SW * x, SH * y); 63 canvas->clipRect(SkRect::MakeLTRB( 64 SkIntToScalar(2), SkIntToScalar(2) 65 , SW - SkIntToScalar(2), SH - SkIntToScalar(2) 66 )); 67 68 SkRandom rand; 69 for (int i = 0; i < N; i++) { 70 SkRect r; 71 rnd_rect(&r, rand); 72 canvas->drawRect(r, paint); 73 } 74 } 75 } 76 } 77 78 private: 79 typedef GM INHERITED; 80 }; 81 82 ////////////////////////////////////////////////////////////////////////////// 83 84 DEF_GM( return new StrokeRectsGM; ) 85 86 } 87