blob: 3323d44b0469e2ac68d1479e9e999ce748aaad09 [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"
Chris Daltoncc13b352021-03-05 14:59:01 -070012#include "include/core/SkRRect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkRect.h"
14#include "include/core/SkSize.h"
15#include "include/core/SkString.h"
16#include "include/core/SkTypes.h"
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000017
18namespace skiagm {
19
20// Draw various width thin rects at 1/8 horizontal pixel increments
21class ThinRectsGM : public GM {
22public:
Chris Daltoncc13b352021-03-05 14:59:01 -070023 ThinRectsGM(bool round) : fRound(round) {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000024 this->setBGColor(0xFF000000);
25 }
26
27protected:
mtklein36352bf2015-03-25 18:17:31 -070028 SkString onShortName() override {
Chris Daltoncc13b352021-03-05 14:59:01 -070029 return SkString(fRound ? "thinroundrects" : "thinrects");
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000030 }
31
mtklein36352bf2015-03-25 18:17:31 -070032 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070033 return SkISize::Make(240, 320);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000034 }
35
mtklein36352bf2015-03-25 18:17:31 -070036 void onDraw(SkCanvas* canvas) override {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000037
38 SkPaint white;
39 white.setColor(SK_ColorWHITE);
40 white.setAntiAlias(true);
41
42 SkPaint green;
43 green.setColor(SK_ColorGREEN);
44 green.setAntiAlias(true);
45
46 for (int i = 0; i < 8; ++i) {
47 canvas->save();
48 canvas->translate(i*0.125f, i*40.0f);
Chris Daltoncc13b352021-03-05 14:59:01 -070049 this->drawVertRects(canvas, white);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000050
51 canvas->translate(40.0f, 0.0f);
Chris Daltoncc13b352021-03-05 14:59:01 -070052 this->drawVertRects(canvas, green);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000053 canvas->restore();
54
55 canvas->save();
56 canvas->translate(80.0f, i*40.0f + i*0.125f);
Chris Daltoncc13b352021-03-05 14:59:01 -070057 this->drawHorizRects(canvas, white);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000058
59 canvas->translate(40.0f, 0.0f);
Chris Daltoncc13b352021-03-05 14:59:01 -070060 this->drawHorizRects(canvas, green);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000061 canvas->restore();
62
63 canvas->save();
skia.committer@gmail.comeafdf122013-05-15 07:01:09 +000064 canvas->translate(160.0f + i*0.125f,
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000065 i*40.0f + i*0.125f);
Chris Daltoncc13b352021-03-05 14:59:01 -070066 this->drawSquares(canvas, white);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000067
68 canvas->translate(40.0f, 0.0f);
Chris Daltoncc13b352021-03-05 14:59:01 -070069 this->drawSquares(canvas, green);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000070 canvas->restore();
71 }
72 }
73
74private:
Chris Daltoncc13b352021-03-05 14:59:01 -070075 void drawVertRects(SkCanvas* canvas, const SkPaint& p) {
mtkleindbfd7ab2016-09-01 11:24:54 -070076 constexpr SkRect vertRects[] = {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000077 { 1, 1, 5.0f, 21 }, // 4 pix wide
78 { 8, 1, 10.0f, 21 }, // 2 pix wide
79 { 13, 1, 14.0f, 21 }, // 1 pix wide
80 { 17, 1, 17.5f, 21 }, // 1/2 pix wide
81 { 21, 1, 21.25f, 21 }, // 1/4 pix wide
82 { 25, 1, 25.125f, 21 }, // 1/8 pix wide
83 { 29, 1, 29.0f, 21 } // 0 pix wide
84 };
85
Chris Daltoncc13b352021-03-05 14:59:01 -070086 static constexpr SkVector radii[4] = {{1/32.f, 2/32.f}, {3/32.f, 1/32.f}, {2/32.f, 3/32.f},
87 {1/32.f, 3/32.f}};
88 SkRRect rrect;
robertphillips@google.com1a095192013-05-14 16:43:02 +000089 for (size_t j = 0; j < SK_ARRAY_COUNT(vertRects); ++j) {
Chris Daltoncc13b352021-03-05 14:59:01 -070090 if (fRound) {
91 rrect.setRectRadii(vertRects[j], radii);
92 canvas->drawRRect(rrect, p);
93 } else {
94 canvas->drawRect(vertRects[j], p);
95 }
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000096 }
97 }
98
Chris Daltoncc13b352021-03-05 14:59:01 -070099 void drawHorizRects(SkCanvas* canvas, const SkPaint& p) {
mtkleindbfd7ab2016-09-01 11:24:54 -0700100 constexpr SkRect horizRects[] = {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000101 { 1, 1, 21, 5.0f }, // 4 pix high
102 { 1, 8, 21, 10.0f }, // 2 pix high
103 { 1, 13, 21, 14.0f }, // 1 pix high
104 { 1, 17, 21, 17.5f }, // 1/2 pix high
105 { 1, 21, 21, 21.25f }, // 1/4 pix high
106 { 1, 25, 21, 25.125f }, // 1/8 pix high
107 { 1, 29, 21, 29.0f } // 0 pix high
108 };
109
Chris Daltoncc13b352021-03-05 14:59:01 -0700110 SkRRect rrect;
robertphillips@google.com1a095192013-05-14 16:43:02 +0000111 for (size_t j = 0; j < SK_ARRAY_COUNT(horizRects); ++j) {
Chris Daltoncc13b352021-03-05 14:59:01 -0700112 if (fRound) {
113 rrect.setNinePatch(horizRects[j], 1/32.f, 2/32.f, 3/32.f, 4/32.f);
114 canvas->drawRRect(rrect, p);
115 } else {
116 canvas->drawRect(horizRects[j], p);
117 }
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000118 }
119 }
120
Chris Daltoncc13b352021-03-05 14:59:01 -0700121 void drawSquares(SkCanvas* canvas, const SkPaint& p) {
mtkleindbfd7ab2016-09-01 11:24:54 -0700122 constexpr SkRect squares[] = {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000123 { 1, 1, 5.0f, 5.0f }, // 4 pix
124 { 8, 8, 10.0f, 10.0f }, // 2 pix
125 { 13, 13, 14.0f, 14.0f }, // 1 pix
126 { 17, 17, 17.5f, 17.5f }, // 1/2 pix
127 { 21, 21, 21.25f, 21.25f }, // 1/4 pix
128 { 25, 25, 25.125f, 25.125f }, // 1/8 pix
129 { 29, 29, 29.0f, 29.0f } // 0 pix
130 };
131
Chris Daltoncc13b352021-03-05 14:59:01 -0700132 SkRRect rrect;
robertphillips@google.com1a095192013-05-14 16:43:02 +0000133 for (size_t j = 0; j < SK_ARRAY_COUNT(squares); ++j) {
Chris Daltoncc13b352021-03-05 14:59:01 -0700134 if (fRound) {
135 rrect.setRectXY(squares[j], 1/32.f, 2/32.f);
136 canvas->drawRRect(rrect, p);
137 } else {
138 canvas->drawRect(squares[j], p);
139 }
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000140 }
141 }
142
Chris Daltoncc13b352021-03-05 14:59:01 -0700143 const bool fRound;
144
John Stiles7571f9e2020-09-02 22:42:33 -0400145 using INHERITED = GM;
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000146};
147
148//////////////////////////////////////////////////////////////////////////////
149
Chris Daltoncc13b352021-03-05 14:59:01 -0700150DEF_GM( return new ThinRectsGM(false); )
151DEF_GM( return new ThinRectsGM(true); )
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000152
John Stilesa6841be2020-08-06 14:11:56 -0400153} // namespace skiagm