blob: fc5743eb743941610561f9f536dae10c6fa3c435 [file] [log] [blame]
reed@google.com3b14dc12011-04-04 14:31:36 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * 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.
reed@google.com3b14dc12011-04-04 14:31:36 +00006 */
7
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@google.com3b14dc12011-04-04 14:31:36 +000010#include "gm.h"
11#include "SkRandom.h"
12
13namespace skiagm {
14
15#define W 400
16#define H 400
17#define N 100
18
mtkleindbfd7ab2016-09-01 11:24:54 -070019constexpr SkScalar SW = SkIntToScalar(W);
20constexpr SkScalar SH = SkIntToScalar(H);
reed@google.com3b14dc12011-04-04 14:31:36 +000021
commit-bot@chromium.orgbc451d22013-12-09 09:14:59 +000022class StrokeRectsGM : public GM {
reed@google.com3b14dc12011-04-04 14:31:36 +000023public:
commit-bot@chromium.orgbc451d22013-12-09 09:14:59 +000024 StrokeRectsGM() {}
rmistry@google.comd6176b02012-08-23 18:14:13 +000025
reed@google.com3b14dc12011-04-04 14:31:36 +000026protected:
commit-bot@chromium.orga90c6802014-04-30 13:20:45 +000027
mtklein36352bf2015-03-25 18:17:31 -070028 SkString onShortName() override {
reed@google.com3b14dc12011-04-04 14:31:36 +000029 return SkString("strokerects");
30 }
31
mtklein36352bf2015-03-25 18:17:31 -070032 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070033 return SkISize::Make(W*2, H*2);
reed@google.com3b14dc12011-04-04 14:31:36 +000034 }
35
scroggof9d61012014-12-15 12:54:51 -080036 static void rnd_rect(SkRect* r, SkRandom& rand) {
reed@google.com3b14dc12011-04-04 14:31:36 +000037 SkScalar x = rand.nextUScalar1() * W;
38 SkScalar y = rand.nextUScalar1() * H;
39 SkScalar w = rand.nextUScalar1() * (W >> 2);
40 SkScalar h = rand.nextUScalar1() * (H >> 2);
epoger@google.com17b78942011-08-26 14:40:38 +000041 SkScalar hoffset = rand.nextSScalar1();
42 SkScalar woffset = rand.nextSScalar1();
rmistry@google.comd6176b02012-08-23 18:14:13 +000043
reed@google.com3b14dc12011-04-04 14:31:36 +000044 r->set(x, y, x + w, y + h);
epoger@google.com17b78942011-08-26 14:40:38 +000045 r->offset(-w/2 + woffset, -h/2 + hoffset);
reed@google.com3b14dc12011-04-04 14:31:36 +000046 }
47
mtklein36352bf2015-03-25 18:17:31 -070048 void onDraw(SkCanvas* canvas) override {
reed@google.com3b14dc12011-04-04 14:31:36 +000049 SkPaint paint;
50 paint.setStyle(SkPaint::kStroke_Style);
51
52 for (int y = 0; y < 2; y++) {
53 paint.setAntiAlias(!!y);
54 for (int x = 0; x < 2; x++) {
55 paint.setStrokeWidth(x * SkIntToScalar(3));
56
57 SkAutoCanvasRestore acr(canvas, true);
58 canvas->translate(SW * x, SH * y);
bungeman@google.com3c14d0f2011-05-20 14:05:03 +000059 canvas->clipRect(SkRect::MakeLTRB(
60 SkIntToScalar(2), SkIntToScalar(2)
61 , SW - SkIntToScalar(2), SH - SkIntToScalar(2)
62 ));
reed@google.com3b14dc12011-04-04 14:31:36 +000063
scroggof9d61012014-12-15 12:54:51 -080064 SkRandom rand;
reed@google.com3b14dc12011-04-04 14:31:36 +000065 for (int i = 0; i < N; i++) {
66 SkRect r;
67 rnd_rect(&r, rand);
68 canvas->drawRect(r, paint);
69 }
70 }
71 }
72 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000073
reed@google.com3b14dc12011-04-04 14:31:36 +000074private:
75 typedef GM INHERITED;
76};
77
78//////////////////////////////////////////////////////////////////////////////
79
commit-bot@chromium.orgbc451d22013-12-09 09:14:59 +000080static GM* MyFactory(void*) { return new StrokeRectsGM; }
reed@google.com3b14dc12011-04-04 14:31:36 +000081static GMRegistry reg(MyFactory);
82
83}