robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "gm/gm.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkCanvas.h" |
| 10 | #include "include/core/SkColor.h" |
Michael Ludwig | 575c921 | 2021-07-13 11:09:52 -0400 | [diff] [blame] | 11 | #include "include/core/SkImage.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 12 | #include "include/core/SkPaint.h" |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 13 | #include "include/core/SkRRect.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 14 | #include "include/core/SkRect.h" |
| 15 | #include "include/core/SkSize.h" |
| 16 | #include "include/core/SkString.h" |
Michael Ludwig | 575c921 | 2021-07-13 11:09:52 -0400 | [diff] [blame] | 17 | #include "include/core/SkSurface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 18 | #include "include/core/SkTypes.h" |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 19 | |
| 20 | namespace skiagm { |
| 21 | |
| 22 | // Draw various width thin rects at 1/8 horizontal pixel increments |
| 23 | class ThinRectsGM : public GM { |
| 24 | public: |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 25 | ThinRectsGM(bool round) : fRound(round) { |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 26 | this->setBGColor(0xFF000000); |
| 27 | } |
| 28 | |
| 29 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 30 | SkString onShortName() override { |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 31 | return SkString(fRound ? "thinroundrects" : "thinrects"); |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 32 | } |
| 33 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 34 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 35 | return SkISize::Make(240, 320); |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 36 | } |
| 37 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 38 | void onDraw(SkCanvas* canvas) override { |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 39 | |
| 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 Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 51 | this->drawVertRects(canvas, white); |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 52 | |
| 53 | canvas->translate(40.0f, 0.0f); |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 54 | this->drawVertRects(canvas, green); |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 55 | canvas->restore(); |
| 56 | |
| 57 | canvas->save(); |
| 58 | canvas->translate(80.0f, i*40.0f + i*0.125f); |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 59 | this->drawHorizRects(canvas, white); |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 60 | |
| 61 | canvas->translate(40.0f, 0.0f); |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 62 | this->drawHorizRects(canvas, green); |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 63 | canvas->restore(); |
| 64 | |
| 65 | canvas->save(); |
skia.committer@gmail.com | eafdf12 | 2013-05-15 07:01:09 +0000 | [diff] [blame] | 66 | canvas->translate(160.0f + i*0.125f, |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 67 | i*40.0f + i*0.125f); |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 68 | this->drawSquares(canvas, white); |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 69 | |
| 70 | canvas->translate(40.0f, 0.0f); |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 71 | this->drawSquares(canvas, green); |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 72 | canvas->restore(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | private: |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 77 | void drawVertRects(SkCanvas* canvas, const SkPaint& p) { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 78 | constexpr SkRect vertRects[] = { |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 79 | { 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 Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 88 | 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.com | 1a09519 | 2013-05-14 16:43:02 +0000 | [diff] [blame] | 91 | for (size_t j = 0; j < SK_ARRAY_COUNT(vertRects); ++j) { |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 92 | if (fRound) { |
| 93 | rrect.setRectRadii(vertRects[j], radii); |
| 94 | canvas->drawRRect(rrect, p); |
| 95 | } else { |
| 96 | canvas->drawRect(vertRects[j], p); |
| 97 | } |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 101 | void drawHorizRects(SkCanvas* canvas, const SkPaint& p) { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 102 | constexpr SkRect horizRects[] = { |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 103 | { 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 Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 112 | SkRRect rrect; |
robertphillips@google.com | 1a09519 | 2013-05-14 16:43:02 +0000 | [diff] [blame] | 113 | for (size_t j = 0; j < SK_ARRAY_COUNT(horizRects); ++j) { |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 114 | 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.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 123 | void drawSquares(SkCanvas* canvas, const SkPaint& p) { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 124 | constexpr SkRect squares[] = { |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 125 | { 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 Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 134 | SkRRect rrect; |
robertphillips@google.com | 1a09519 | 2013-05-14 16:43:02 +0000 | [diff] [blame] | 135 | for (size_t j = 0; j < SK_ARRAY_COUNT(squares); ++j) { |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 136 | 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.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 145 | const bool fRound; |
| 146 | |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 147 | using INHERITED = GM; |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | ////////////////////////////////////////////////////////////////////////////// |
| 151 | |
Chris Dalton | cc13b35 | 2021-03-05 14:59:01 -0700 | [diff] [blame] | 152 | DEF_GM( return new ThinRectsGM(false); ) |
| 153 | DEF_GM( return new ThinRectsGM(true); ) |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 154 | |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 155 | } // namespace skiagm |
Michael Ludwig | 575c921 | 2021-07-13 11:09:52 -0400 | [diff] [blame] | 156 | |
| 157 | DEF_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 | } |