blob: ad0ba7d906ecfc8ee08525137b9200c12410da2a [file] [log] [blame]
robertphillips@google.comdd3f3652013-05-14 16:37:31 +00001/*
2 * Copyright 2013 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -04009#include "include/core/SkCanvas.h"
10#include "include/core/SkColor.h"
11#include "include/core/SkPaint.h"
12#include "include/core/SkRect.h"
13#include "include/core/SkSize.h"
14#include "include/core/SkString.h"
15#include "include/core/SkTypes.h"
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000016
17namespace skiagm {
18
19// Draw various width thin rects at 1/8 horizontal pixel increments
20class ThinRectsGM : public GM {
21public:
22 ThinRectsGM() {
23 this->setBGColor(0xFF000000);
24 }
25
26protected:
mtklein36352bf2015-03-25 18:17:31 -070027 SkString onShortName() override {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000028 return SkString("thinrects");
29 }
30
mtklein36352bf2015-03-25 18:17:31 -070031 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070032 return SkISize::Make(240, 320);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000033 }
34
mtklein36352bf2015-03-25 18:17:31 -070035 void onDraw(SkCanvas* canvas) override {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000036
37 SkPaint white;
38 white.setColor(SK_ColorWHITE);
39 white.setAntiAlias(true);
40
41 SkPaint green;
42 green.setColor(SK_ColorGREEN);
43 green.setAntiAlias(true);
44
45 for (int i = 0; i < 8; ++i) {
46 canvas->save();
47 canvas->translate(i*0.125f, i*40.0f);
48 DrawVertRects(canvas, white);
49
50 canvas->translate(40.0f, 0.0f);
51 DrawVertRects(canvas, green);
52 canvas->restore();
53
54 canvas->save();
55 canvas->translate(80.0f, i*40.0f + i*0.125f);
56 DrawHorizRects(canvas, white);
57
58 canvas->translate(40.0f, 0.0f);
59 DrawHorizRects(canvas, green);
60 canvas->restore();
61
62 canvas->save();
skia.committer@gmail.comeafdf122013-05-15 07:01:09 +000063 canvas->translate(160.0f + i*0.125f,
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000064 i*40.0f + i*0.125f);
65 DrawSquares(canvas, white);
66
67 canvas->translate(40.0f, 0.0f);
68 DrawSquares(canvas, green);
69 canvas->restore();
70 }
71 }
72
73private:
74 static void DrawVertRects(SkCanvas* canvas, const SkPaint& p) {
mtkleindbfd7ab2016-09-01 11:24:54 -070075 constexpr SkRect vertRects[] = {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000076 { 1, 1, 5.0f, 21 }, // 4 pix wide
77 { 8, 1, 10.0f, 21 }, // 2 pix wide
78 { 13, 1, 14.0f, 21 }, // 1 pix wide
79 { 17, 1, 17.5f, 21 }, // 1/2 pix wide
80 { 21, 1, 21.25f, 21 }, // 1/4 pix wide
81 { 25, 1, 25.125f, 21 }, // 1/8 pix wide
82 { 29, 1, 29.0f, 21 } // 0 pix wide
83 };
84
robertphillips@google.com1a095192013-05-14 16:43:02 +000085 for (size_t j = 0; j < SK_ARRAY_COUNT(vertRects); ++j) {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000086 canvas->drawRect(vertRects[j], p);
87 }
88 }
89
90 static void DrawHorizRects(SkCanvas* canvas, const SkPaint& p) {
mtkleindbfd7ab2016-09-01 11:24:54 -070091 constexpr SkRect horizRects[] = {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000092 { 1, 1, 21, 5.0f }, // 4 pix high
93 { 1, 8, 21, 10.0f }, // 2 pix high
94 { 1, 13, 21, 14.0f }, // 1 pix high
95 { 1, 17, 21, 17.5f }, // 1/2 pix high
96 { 1, 21, 21, 21.25f }, // 1/4 pix high
97 { 1, 25, 21, 25.125f }, // 1/8 pix high
98 { 1, 29, 21, 29.0f } // 0 pix high
99 };
100
robertphillips@google.com1a095192013-05-14 16:43:02 +0000101 for (size_t j = 0; j < SK_ARRAY_COUNT(horizRects); ++j) {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000102 canvas->drawRect(horizRects[j], p);
103 }
104 }
105
106 static void DrawSquares(SkCanvas* canvas, const SkPaint& p) {
mtkleindbfd7ab2016-09-01 11:24:54 -0700107 constexpr SkRect squares[] = {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000108 { 1, 1, 5.0f, 5.0f }, // 4 pix
109 { 8, 8, 10.0f, 10.0f }, // 2 pix
110 { 13, 13, 14.0f, 14.0f }, // 1 pix
111 { 17, 17, 17.5f, 17.5f }, // 1/2 pix
112 { 21, 21, 21.25f, 21.25f }, // 1/4 pix
113 { 25, 25, 25.125f, 25.125f }, // 1/8 pix
114 { 29, 29, 29.0f, 29.0f } // 0 pix
115 };
116
robertphillips@google.com1a095192013-05-14 16:43:02 +0000117 for (size_t j = 0; j < SK_ARRAY_COUNT(squares); ++j) {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000118 canvas->drawRect(squares[j], p);
119 }
120 }
121
122 typedef GM INHERITED;
123};
124
125//////////////////////////////////////////////////////////////////////////////
126
Hal Canarye964c182019-01-23 10:22:01 -0500127DEF_GM( return new ThinRectsGM; )
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000128
129}