blob: 16a563b419c35ba9557dabc66591d294c4567332 [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"
Michael Ludwig575c9212021-07-13 11:09:52 -040011#include "include/core/SkImage.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040012#include "include/core/SkPaint.h"
Chris Daltoncc13b352021-03-05 14:59:01 -070013#include "include/core/SkRRect.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040014#include "include/core/SkRect.h"
15#include "include/core/SkSize.h"
16#include "include/core/SkString.h"
Michael Ludwig575c9212021-07-13 11:09:52 -040017#include "include/core/SkSurface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040018#include "include/core/SkTypes.h"
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000019
20namespace skiagm {
21
22// Draw various width thin rects at 1/8 horizontal pixel increments
23class ThinRectsGM : public GM {
24public:
Chris Daltoncc13b352021-03-05 14:59:01 -070025 ThinRectsGM(bool round) : fRound(round) {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000026 this->setBGColor(0xFF000000);
27 }
28
29protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000030 SkString getName() const override { return SkString(fRound ? "thinroundrects" : "thinrects"); }
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000031
Leandro Lovisolo8f023882023-08-15 21:13:52 +000032 SkISize getISize() override { return SkISize::Make(240, 320); }
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000033
mtklein36352bf2015-03-25 18:17:31 -070034 void onDraw(SkCanvas* canvas) override {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000035
36 SkPaint white;
37 white.setColor(SK_ColorWHITE);
38 white.setAntiAlias(true);
39
40 SkPaint green;
41 green.setColor(SK_ColorGREEN);
42 green.setAntiAlias(true);
43
44 for (int i = 0; i < 8; ++i) {
45 canvas->save();
46 canvas->translate(i*0.125f, i*40.0f);
Chris Daltoncc13b352021-03-05 14:59:01 -070047 this->drawVertRects(canvas, white);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000048
49 canvas->translate(40.0f, 0.0f);
Chris Daltoncc13b352021-03-05 14:59:01 -070050 this->drawVertRects(canvas, green);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000051 canvas->restore();
52
53 canvas->save();
54 canvas->translate(80.0f, i*40.0f + i*0.125f);
Chris Daltoncc13b352021-03-05 14:59:01 -070055 this->drawHorizRects(canvas, white);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000056
57 canvas->translate(40.0f, 0.0f);
Chris Daltoncc13b352021-03-05 14:59:01 -070058 this->drawHorizRects(canvas, green);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000059 canvas->restore();
60
61 canvas->save();
skia.committer@gmail.comeafdf122013-05-15 07:01:09 +000062 canvas->translate(160.0f + i*0.125f,
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000063 i*40.0f + i*0.125f);
Chris Daltoncc13b352021-03-05 14:59:01 -070064 this->drawSquares(canvas, white);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000065
66 canvas->translate(40.0f, 0.0f);
Chris Daltoncc13b352021-03-05 14:59:01 -070067 this->drawSquares(canvas, green);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000068 canvas->restore();
69 }
70 }
71
72private:
Chris Daltoncc13b352021-03-05 14:59:01 -070073 void drawVertRects(SkCanvas* canvas, const SkPaint& p) {
mtkleindbfd7ab2016-09-01 11:24:54 -070074 constexpr SkRect vertRects[] = {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000075 { 1, 1, 5.0f, 21 }, // 4 pix wide
76 { 8, 1, 10.0f, 21 }, // 2 pix wide
77 { 13, 1, 14.0f, 21 }, // 1 pix wide
78 { 17, 1, 17.5f, 21 }, // 1/2 pix wide
79 { 21, 1, 21.25f, 21 }, // 1/4 pix wide
80 { 25, 1, 25.125f, 21 }, // 1/8 pix wide
81 { 29, 1, 29.0f, 21 } // 0 pix wide
82 };
83
Chris Daltoncc13b352021-03-05 14:59:01 -070084 static constexpr SkVector radii[4] = {{1/32.f, 2/32.f}, {3/32.f, 1/32.f}, {2/32.f, 3/32.f},
85 {1/32.f, 3/32.f}};
86 SkRRect rrect;
Herb Derbyc37b3862022-06-21 09:49:17 -040087 for (size_t j = 0; j < std::size(vertRects); ++j) {
Chris Daltoncc13b352021-03-05 14:59:01 -070088 if (fRound) {
89 rrect.setRectRadii(vertRects[j], radii);
90 canvas->drawRRect(rrect, p);
91 } else {
92 canvas->drawRect(vertRects[j], p);
93 }
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000094 }
95 }
96
Chris Daltoncc13b352021-03-05 14:59:01 -070097 void drawHorizRects(SkCanvas* canvas, const SkPaint& p) {
mtkleindbfd7ab2016-09-01 11:24:54 -070098 constexpr SkRect horizRects[] = {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000099 { 1, 1, 21, 5.0f }, // 4 pix high
100 { 1, 8, 21, 10.0f }, // 2 pix high
101 { 1, 13, 21, 14.0f }, // 1 pix high
102 { 1, 17, 21, 17.5f }, // 1/2 pix high
103 { 1, 21, 21, 21.25f }, // 1/4 pix high
104 { 1, 25, 21, 25.125f }, // 1/8 pix high
105 { 1, 29, 21, 29.0f } // 0 pix high
106 };
107
Chris Daltoncc13b352021-03-05 14:59:01 -0700108 SkRRect rrect;
Herb Derbyc37b3862022-06-21 09:49:17 -0400109 for (size_t j = 0; j < std::size(horizRects); ++j) {
Chris Daltoncc13b352021-03-05 14:59:01 -0700110 if (fRound) {
111 rrect.setNinePatch(horizRects[j], 1/32.f, 2/32.f, 3/32.f, 4/32.f);
112 canvas->drawRRect(rrect, p);
113 } else {
114 canvas->drawRect(horizRects[j], p);
115 }
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000116 }
117 }
118
Chris Daltoncc13b352021-03-05 14:59:01 -0700119 void drawSquares(SkCanvas* canvas, const SkPaint& p) {
mtkleindbfd7ab2016-09-01 11:24:54 -0700120 constexpr SkRect squares[] = {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000121 { 1, 1, 5.0f, 5.0f }, // 4 pix
122 { 8, 8, 10.0f, 10.0f }, // 2 pix
123 { 13, 13, 14.0f, 14.0f }, // 1 pix
124 { 17, 17, 17.5f, 17.5f }, // 1/2 pix
125 { 21, 21, 21.25f, 21.25f }, // 1/4 pix
126 { 25, 25, 25.125f, 25.125f }, // 1/8 pix
127 { 29, 29, 29.0f, 29.0f } // 0 pix
128 };
129
Chris Daltoncc13b352021-03-05 14:59:01 -0700130 SkRRect rrect;
Herb Derbyc37b3862022-06-21 09:49:17 -0400131 for (size_t j = 0; j < std::size(squares); ++j) {
Chris Daltoncc13b352021-03-05 14:59:01 -0700132 if (fRound) {
133 rrect.setRectXY(squares[j], 1/32.f, 2/32.f);
134 canvas->drawRRect(rrect, p);
135 } else {
136 canvas->drawRect(squares[j], p);
137 }
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000138 }
139 }
140
Chris Daltoncc13b352021-03-05 14:59:01 -0700141 const bool fRound;
142
John Stiles7571f9e2020-09-02 22:42:33 -0400143 using INHERITED = GM;
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000144};
145
146//////////////////////////////////////////////////////////////////////////////
147
Chris Daltoncc13b352021-03-05 14:59:01 -0700148DEF_GM( return new ThinRectsGM(false); )
149DEF_GM( return new ThinRectsGM(true); )
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000150
John Stilesa6841be2020-08-06 14:11:56 -0400151} // namespace skiagm
Michael Ludwig575c9212021-07-13 11:09:52 -0400152
153DEF_SIMPLE_GM_CAN_FAIL(clipped_thinrect, canvas, errorMsg, 256, 256) {
154 auto zoomed = canvas->makeSurface(canvas->imageInfo().makeWH(10, 10));
155 if (!zoomed) {
156 errorMsg->printf("makeSurface not supported");
157 return skiagm::DrawResult::kSkip;
158 }
159 auto zoomedCanvas = zoomed->getCanvas();
160
161 SkPaint p;
162 p.setColor(SK_ColorRED);
163 p.setAntiAlias(true);
164 p.setStyle(SkPaint::kFill_Style);
165 zoomedCanvas->save();
166 zoomedCanvas->clipRect(SkRect::MakeXYWH(0, 5, 256, 10), true /*doAntialias*/);
167 zoomedCanvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 5.5), p);
168 zoomedCanvas->restore();
169
170 // Zoom-in. Should see one line of red representing zoomed in 1/2px coverage and *not*
171 // two lines of varying coverage from hairline rendering.
172 auto img = zoomed->makeImageSnapshot();
173 canvas->drawImageRect(img, SkRect::MakeXYWH(0, 10, 200, 200), SkSamplingOptions());
174 return skiagm::DrawResult::kOk;
175}