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" |
| 12 | #include "include/core/SkRect.h" |
| 13 | #include "include/core/SkSize.h" |
| 14 | #include "include/core/SkString.h" |
| 15 | #include "include/core/SkTypes.h" |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 16 | |
| 17 | namespace skiagm { |
| 18 | |
| 19 | // Draw various width thin rects at 1/8 horizontal pixel increments |
| 20 | class ThinRectsGM : public GM { |
| 21 | public: |
| 22 | ThinRectsGM() { |
| 23 | this->setBGColor(0xFF000000); |
| 24 | } |
| 25 | |
| 26 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 27 | SkString onShortName() override { |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 28 | return SkString("thinrects"); |
| 29 | } |
| 30 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 31 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 32 | return SkISize::Make(240, 320); |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 33 | } |
| 34 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 35 | void onDraw(SkCanvas* canvas) override { |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 36 | |
| 37 | SkPaint white; |
| 38 | white.setColor(SK_ColorWHITE); |
| 39 | white.setAntiAlias(true); |
| 40 | |
| 41 | SkPaint green; |
| 42 | green.setColor(SK_ColorGREEN); |
| 43 | green.setAntiAlias(true); |
| 44 | |
| 45 | for (int i = 0; i < 8; ++i) { |
| 46 | canvas->save(); |
| 47 | canvas->translate(i*0.125f, i*40.0f); |
| 48 | DrawVertRects(canvas, white); |
| 49 | |
| 50 | canvas->translate(40.0f, 0.0f); |
| 51 | DrawVertRects(canvas, green); |
| 52 | canvas->restore(); |
| 53 | |
| 54 | canvas->save(); |
| 55 | canvas->translate(80.0f, i*40.0f + i*0.125f); |
| 56 | DrawHorizRects(canvas, white); |
| 57 | |
| 58 | canvas->translate(40.0f, 0.0f); |
| 59 | DrawHorizRects(canvas, green); |
| 60 | canvas->restore(); |
| 61 | |
| 62 | canvas->save(); |
skia.committer@gmail.com | eafdf12 | 2013-05-15 07:01:09 +0000 | [diff] [blame] | 63 | canvas->translate(160.0f + i*0.125f, |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 64 | i*40.0f + i*0.125f); |
| 65 | DrawSquares(canvas, white); |
| 66 | |
| 67 | canvas->translate(40.0f, 0.0f); |
| 68 | DrawSquares(canvas, green); |
| 69 | canvas->restore(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | static void DrawVertRects(SkCanvas* canvas, const SkPaint& p) { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 75 | constexpr SkRect vertRects[] = { |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 76 | { 1, 1, 5.0f, 21 }, // 4 pix wide |
| 77 | { 8, 1, 10.0f, 21 }, // 2 pix wide |
| 78 | { 13, 1, 14.0f, 21 }, // 1 pix wide |
| 79 | { 17, 1, 17.5f, 21 }, // 1/2 pix wide |
| 80 | { 21, 1, 21.25f, 21 }, // 1/4 pix wide |
| 81 | { 25, 1, 25.125f, 21 }, // 1/8 pix wide |
| 82 | { 29, 1, 29.0f, 21 } // 0 pix wide |
| 83 | }; |
| 84 | |
robertphillips@google.com | 1a09519 | 2013-05-14 16:43:02 +0000 | [diff] [blame] | 85 | for (size_t j = 0; j < SK_ARRAY_COUNT(vertRects); ++j) { |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 86 | canvas->drawRect(vertRects[j], p); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static void DrawHorizRects(SkCanvas* canvas, const SkPaint& p) { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 91 | constexpr SkRect horizRects[] = { |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 92 | { 1, 1, 21, 5.0f }, // 4 pix high |
| 93 | { 1, 8, 21, 10.0f }, // 2 pix high |
| 94 | { 1, 13, 21, 14.0f }, // 1 pix high |
| 95 | { 1, 17, 21, 17.5f }, // 1/2 pix high |
| 96 | { 1, 21, 21, 21.25f }, // 1/4 pix high |
| 97 | { 1, 25, 21, 25.125f }, // 1/8 pix high |
| 98 | { 1, 29, 21, 29.0f } // 0 pix high |
| 99 | }; |
| 100 | |
robertphillips@google.com | 1a09519 | 2013-05-14 16:43:02 +0000 | [diff] [blame] | 101 | for (size_t j = 0; j < SK_ARRAY_COUNT(horizRects); ++j) { |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 102 | canvas->drawRect(horizRects[j], p); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static void DrawSquares(SkCanvas* canvas, const SkPaint& p) { |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 107 | constexpr SkRect squares[] = { |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 108 | { 1, 1, 5.0f, 5.0f }, // 4 pix |
| 109 | { 8, 8, 10.0f, 10.0f }, // 2 pix |
| 110 | { 13, 13, 14.0f, 14.0f }, // 1 pix |
| 111 | { 17, 17, 17.5f, 17.5f }, // 1/2 pix |
| 112 | { 21, 21, 21.25f, 21.25f }, // 1/4 pix |
| 113 | { 25, 25, 25.125f, 25.125f }, // 1/8 pix |
| 114 | { 29, 29, 29.0f, 29.0f } // 0 pix |
| 115 | }; |
| 116 | |
robertphillips@google.com | 1a09519 | 2013-05-14 16:43:02 +0000 | [diff] [blame] | 117 | for (size_t j = 0; j < SK_ARRAY_COUNT(squares); ++j) { |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 118 | canvas->drawRect(squares[j], p); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | typedef GM INHERITED; |
| 123 | }; |
| 124 | |
| 125 | ////////////////////////////////////////////////////////////////////////////// |
| 126 | |
Hal Canary | e964c18 | 2019-01-23 10:22:01 -0500 | [diff] [blame] | 127 | DEF_GM( return new ThinRectsGM; ) |
robertphillips@google.com | dd3f365 | 2013-05-14 16:37:31 +0000 | [diff] [blame] | 128 | |
| 129 | } |