blob: faf2be0eb3a4201981b77c312326650ed295fd52 [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:
mtklein36352bf2015-03-25 18:17:31 -070030 SkString onShortName() override {
Chris Daltoncc13b352021-03-05 14:59:01 -070031 return SkString(fRound ? "thinroundrects" : "thinrects");
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000032 }
33
mtklein36352bf2015-03-25 18:17:31 -070034 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070035 return SkISize::Make(240, 320);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000036 }
37
mtklein36352bf2015-03-25 18:17:31 -070038 void onDraw(SkCanvas* canvas) override {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000039
40 SkPaint white;
41 white.setColor(SK_ColorWHITE);
42 white.setAntiAlias(true);
43
44 SkPaint green;
45 green.setColor(SK_ColorGREEN);
46 green.setAntiAlias(true);
47
48 for (int i = 0; i < 8; ++i) {
49 canvas->save();
50 canvas->translate(i*0.125f, i*40.0f);
Chris Daltoncc13b352021-03-05 14:59:01 -070051 this->drawVertRects(canvas, white);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000052
53 canvas->translate(40.0f, 0.0f);
Chris Daltoncc13b352021-03-05 14:59:01 -070054 this->drawVertRects(canvas, green);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000055 canvas->restore();
56
57 canvas->save();
58 canvas->translate(80.0f, i*40.0f + i*0.125f);
Chris Daltoncc13b352021-03-05 14:59:01 -070059 this->drawHorizRects(canvas, white);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000060
61 canvas->translate(40.0f, 0.0f);
Chris Daltoncc13b352021-03-05 14:59:01 -070062 this->drawHorizRects(canvas, green);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000063 canvas->restore();
64
65 canvas->save();
skia.committer@gmail.comeafdf122013-05-15 07:01:09 +000066 canvas->translate(160.0f + i*0.125f,
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000067 i*40.0f + i*0.125f);
Chris Daltoncc13b352021-03-05 14:59:01 -070068 this->drawSquares(canvas, white);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000069
70 canvas->translate(40.0f, 0.0f);
Chris Daltoncc13b352021-03-05 14:59:01 -070071 this->drawSquares(canvas, green);
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000072 canvas->restore();
73 }
74 }
75
76private:
Chris Daltoncc13b352021-03-05 14:59:01 -070077 void drawVertRects(SkCanvas* canvas, const SkPaint& p) {
mtkleindbfd7ab2016-09-01 11:24:54 -070078 constexpr SkRect vertRects[] = {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000079 { 1, 1, 5.0f, 21 }, // 4 pix wide
80 { 8, 1, 10.0f, 21 }, // 2 pix wide
81 { 13, 1, 14.0f, 21 }, // 1 pix wide
82 { 17, 1, 17.5f, 21 }, // 1/2 pix wide
83 { 21, 1, 21.25f, 21 }, // 1/4 pix wide
84 { 25, 1, 25.125f, 21 }, // 1/8 pix wide
85 { 29, 1, 29.0f, 21 } // 0 pix wide
86 };
87
Chris Daltoncc13b352021-03-05 14:59:01 -070088 static constexpr SkVector radii[4] = {{1/32.f, 2/32.f}, {3/32.f, 1/32.f}, {2/32.f, 3/32.f},
89 {1/32.f, 3/32.f}};
90 SkRRect rrect;
robertphillips@google.com1a095192013-05-14 16:43:02 +000091 for (size_t j = 0; j < SK_ARRAY_COUNT(vertRects); ++j) {
Chris Daltoncc13b352021-03-05 14:59:01 -070092 if (fRound) {
93 rrect.setRectRadii(vertRects[j], radii);
94 canvas->drawRRect(rrect, p);
95 } else {
96 canvas->drawRect(vertRects[j], p);
97 }
robertphillips@google.comdd3f3652013-05-14 16:37:31 +000098 }
99 }
100
Chris Daltoncc13b352021-03-05 14:59:01 -0700101 void drawHorizRects(SkCanvas* canvas, const SkPaint& p) {
mtkleindbfd7ab2016-09-01 11:24:54 -0700102 constexpr SkRect horizRects[] = {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000103 { 1, 1, 21, 5.0f }, // 4 pix high
104 { 1, 8, 21, 10.0f }, // 2 pix high
105 { 1, 13, 21, 14.0f }, // 1 pix high
106 { 1, 17, 21, 17.5f }, // 1/2 pix high
107 { 1, 21, 21, 21.25f }, // 1/4 pix high
108 { 1, 25, 21, 25.125f }, // 1/8 pix high
109 { 1, 29, 21, 29.0f } // 0 pix high
110 };
111
Chris Daltoncc13b352021-03-05 14:59:01 -0700112 SkRRect rrect;
robertphillips@google.com1a095192013-05-14 16:43:02 +0000113 for (size_t j = 0; j < SK_ARRAY_COUNT(horizRects); ++j) {
Chris Daltoncc13b352021-03-05 14:59:01 -0700114 if (fRound) {
115 rrect.setNinePatch(horizRects[j], 1/32.f, 2/32.f, 3/32.f, 4/32.f);
116 canvas->drawRRect(rrect, p);
117 } else {
118 canvas->drawRect(horizRects[j], p);
119 }
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000120 }
121 }
122
Chris Daltoncc13b352021-03-05 14:59:01 -0700123 void drawSquares(SkCanvas* canvas, const SkPaint& p) {
mtkleindbfd7ab2016-09-01 11:24:54 -0700124 constexpr SkRect squares[] = {
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000125 { 1, 1, 5.0f, 5.0f }, // 4 pix
126 { 8, 8, 10.0f, 10.0f }, // 2 pix
127 { 13, 13, 14.0f, 14.0f }, // 1 pix
128 { 17, 17, 17.5f, 17.5f }, // 1/2 pix
129 { 21, 21, 21.25f, 21.25f }, // 1/4 pix
130 { 25, 25, 25.125f, 25.125f }, // 1/8 pix
131 { 29, 29, 29.0f, 29.0f } // 0 pix
132 };
133
Chris Daltoncc13b352021-03-05 14:59:01 -0700134 SkRRect rrect;
robertphillips@google.com1a095192013-05-14 16:43:02 +0000135 for (size_t j = 0; j < SK_ARRAY_COUNT(squares); ++j) {
Chris Daltoncc13b352021-03-05 14:59:01 -0700136 if (fRound) {
137 rrect.setRectXY(squares[j], 1/32.f, 2/32.f);
138 canvas->drawRRect(rrect, p);
139 } else {
140 canvas->drawRect(squares[j], p);
141 }
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000142 }
143 }
144
Chris Daltoncc13b352021-03-05 14:59:01 -0700145 const bool fRound;
146
John Stiles7571f9e2020-09-02 22:42:33 -0400147 using INHERITED = GM;
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000148};
149
150//////////////////////////////////////////////////////////////////////////////
151
Chris Daltoncc13b352021-03-05 14:59:01 -0700152DEF_GM( return new ThinRectsGM(false); )
153DEF_GM( return new ThinRectsGM(true); )
robertphillips@google.comdd3f3652013-05-14 16:37:31 +0000154
John Stilesa6841be2020-08-06 14:11:56 -0400155} // namespace skiagm
Michael Ludwig575c9212021-07-13 11:09:52 -0400156
157DEF_SIMPLE_GM_CAN_FAIL(clipped_thinrect, canvas, errorMsg, 256, 256) {
158 auto zoomed = canvas->makeSurface(canvas->imageInfo().makeWH(10, 10));
159 if (!zoomed) {
160 errorMsg->printf("makeSurface not supported");
161 return skiagm::DrawResult::kSkip;
162 }
163 auto zoomedCanvas = zoomed->getCanvas();
164
165 SkPaint p;
166 p.setColor(SK_ColorRED);
167 p.setAntiAlias(true);
168 p.setStyle(SkPaint::kFill_Style);
169 zoomedCanvas->save();
170 zoomedCanvas->clipRect(SkRect::MakeXYWH(0, 5, 256, 10), true /*doAntialias*/);
171 zoomedCanvas->drawRect(SkRect::MakeXYWH(0, 0, 100, 5.5), p);
172 zoomedCanvas->restore();
173
174 // Zoom-in. Should see one line of red representing zoomed in 1/2px coverage and *not*
175 // two lines of varying coverage from hairline rendering.
176 auto img = zoomed->makeImageSnapshot();
177 canvas->drawImageRect(img, SkRect::MakeXYWH(0, 10, 200, 200), SkSamplingOptions());
178 return skiagm::DrawResult::kOk;
179}