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