bsalomon | 87cbcf3 | 2015-04-22 08:51:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
| 8 | #include "gm.h" |
| 9 | |
| 10 | #include "SkShader.h" |
| 11 | |
| 12 | // This class of GMs test how edges/verts snap near rounding boundaries in device space without |
| 13 | // anti-aliaing. |
| 14 | class PixelSnapGM : public skiagm::GM { |
| 15 | public: |
| 16 | PixelSnapGM() {} |
| 17 | |
| 18 | protected: |
| 19 | // kTrans should be even or checkboards wont agree in different test cases. |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 20 | static constexpr int kTrans = 14; |
| 21 | static constexpr int kLabelPad = 4; |
bsalomon | 87cbcf3 | 2015-04-22 08:51:38 -0700 | [diff] [blame] | 22 | // The inverse of this value should be a perfect SkScalar. |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 23 | static constexpr int kSubPixelSteps = 8; |
| 24 | static constexpr int kLabelTextSize = 9; |
bsalomon | 87cbcf3 | 2015-04-22 08:51:38 -0700 | [diff] [blame] | 25 | |
bungeman | 99fe822 | 2015-08-20 07:57:51 -0700 | [diff] [blame] | 26 | static_assert(kSubPixelSteps < 99, "label_offset_too_small"); |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 27 | static constexpr int kLabelOffsetX = 2 * kLabelTextSize + kLabelPad; |
| 28 | static constexpr int kLabelOffsetY = kLabelTextSize + kLabelPad; |
bsalomon | c6534a2 | 2015-04-22 11:34:46 -0700 | [diff] [blame] | 29 | |
bsalomon | 87cbcf3 | 2015-04-22 08:51:38 -0700 | [diff] [blame] | 30 | SkISize onISize() override { |
bsalomon | c6534a2 | 2015-04-22 11:34:46 -0700 | [diff] [blame] | 31 | return SkISize::Make((kSubPixelSteps + 1) * kTrans + kLabelOffsetX + kLabelPad, |
| 32 | (kSubPixelSteps + 1) * kTrans + kLabelOffsetY + kLabelPad); |
bsalomon | 87cbcf3 | 2015-04-22 08:51:38 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | void onDraw(SkCanvas* canvas) override { |
| 36 | SkPaint bgPaint; |
| 37 | bgPaint.setShader( |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 38 | sk_tool_utils::create_checkerboard_shader( |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 39 | sk_tool_utils::color_to_565(0xFFAAAAAA), |
reed | 8a21c9f | 2016-03-08 18:50:00 -0800 | [diff] [blame] | 40 | sk_tool_utils::color_to_565(0xFF777777), 1)); |
bsalomon | 87cbcf3 | 2015-04-22 08:51:38 -0700 | [diff] [blame] | 41 | canvas->drawPaint(bgPaint); |
| 42 | |
| 43 | SkString offset; |
| 44 | SkPaint labelPaint; |
| 45 | labelPaint.setAntiAlias(true); |
| 46 | labelPaint.setColor(SK_ColorWHITE); |
| 47 | labelPaint.setTextSize(SkIntToScalar(kLabelTextSize)); |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 48 | sk_tool_utils::set_portable_typeface(&labelPaint); |
bsalomon | 87cbcf3 | 2015-04-22 08:51:38 -0700 | [diff] [blame] | 49 | SkPaint linePaint; |
| 50 | linePaint.setColor(SK_ColorWHITE); |
| 51 | |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 52 | // Draw row labels |
| 53 | canvas->save(); |
| 54 | canvas->translate(0, SkIntToScalar(kLabelOffsetY)); |
| 55 | for (int i = 0; i <= kSubPixelSteps; ++i) { |
| 56 | offset.printf("%d", i); |
| 57 | canvas->drawText(offset.c_str(), offset.size(), |
| 58 | 0, i * kTrans + labelPaint.getTextSize(), |
| 59 | labelPaint); |
| 60 | } |
| 61 | canvas->restore(); |
bsalomon | 87cbcf3 | 2015-04-22 08:51:38 -0700 | [diff] [blame] | 62 | |
caryclark | f597c42 | 2015-07-28 10:37:53 -0700 | [diff] [blame] | 63 | // Draw col labels |
| 64 | canvas->save(); |
| 65 | canvas->translate(SkIntToScalar(kLabelOffsetX), 0); |
| 66 | for (int i = 0; i <= kSubPixelSteps; ++i) { |
| 67 | offset.printf("%d", i); |
| 68 | canvas->drawText(offset.c_str(), offset.size(), |
| 69 | i * SkIntToScalar(kTrans), labelPaint.getTextSize(), |
| 70 | labelPaint); |
| 71 | } |
| 72 | canvas->restore(); |
bsalomon | 87cbcf3 | 2015-04-22 08:51:38 -0700 | [diff] [blame] | 73 | |
bsalomon | c6534a2 | 2015-04-22 11:34:46 -0700 | [diff] [blame] | 74 | canvas->translate(SkIntToScalar(kLabelOffsetX), SkIntToScalar(kLabelOffsetY)); |
bsalomon | 87cbcf3 | 2015-04-22 08:51:38 -0700 | [diff] [blame] | 75 | |
| 76 | // Draw test case grid lines (Draw them all at pixel centers to hopefully avoid any |
| 77 | // snapping issues). |
| 78 | for (int i = 0; i <= kSubPixelSteps + 1; ++i) { |
| 79 | canvas->drawLine(0.5f, |
| 80 | i * SkIntToScalar(kTrans) + 0.5f, |
| 81 | SkIntToScalar(kTrans) * (kSubPixelSteps + 1) + 0.5f, |
| 82 | i * SkIntToScalar(kTrans) + 0.5f, |
| 83 | linePaint); |
| 84 | canvas->drawLine(i * SkIntToScalar(kTrans) + 0.5f, |
| 85 | 0.5f, |
| 86 | i * SkIntToScalar(kTrans) + 0.5f, |
| 87 | SkIntToScalar(kTrans) * (kSubPixelSteps + 1) + 0.5f, |
| 88 | linePaint); |
| 89 | } |
| 90 | |
| 91 | for (int i = 0; i <= kSubPixelSteps; ++i) { |
| 92 | for (int j = 0; j <= kSubPixelSteps; ++j) { |
| 93 | canvas->save(); |
| 94 | // +1's account for the grid lines around each test case. |
| 95 | canvas->translate(j * (kTrans + 1.f/kSubPixelSteps) + 1, |
| 96 | i * (kTrans + 1.f/kSubPixelSteps) + 1); |
| 97 | this->drawElement(canvas); |
| 98 | canvas->restore(); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | virtual void drawElement(SkCanvas*) = 0; |
| 104 | |
| 105 | private: |
| 106 | typedef skiagm::GM INHERITED; |
| 107 | }; |
| 108 | |
| 109 | class PointSnapGM : public PixelSnapGM { |
| 110 | protected: |
| 111 | SkString onShortName() override { return SkString("pixel_snap_point"); } |
| 112 | void drawElement(SkCanvas* canvas) override { canvas->drawPoint(1, 1, SK_ColorBLUE); } |
| 113 | |
| 114 | private: |
| 115 | typedef PixelSnapGM INHERITED; |
| 116 | }; |
| 117 | |
| 118 | class LineSnapGM : public PixelSnapGM { |
| 119 | protected: |
| 120 | SkString onShortName() override { return SkString("pixel_snap_line"); } |
| 121 | void drawElement(SkCanvas* canvas) override { |
| 122 | SkPaint paint; |
| 123 | paint.setColor(SK_ColorGREEN); |
| 124 | // Draw a horizontal and vertical line, each length 3. |
| 125 | canvas->drawLine(1, 1, 4, 1, paint); |
| 126 | canvas->drawLine(6, 1, 6, 4, paint); |
| 127 | } |
| 128 | |
| 129 | private: |
| 130 | typedef PixelSnapGM INHERITED; |
| 131 | }; |
| 132 | |
| 133 | class RectSnapGM : public PixelSnapGM { |
| 134 | protected: |
| 135 | SkString onShortName() override { return SkString("pixel_snap_rect"); } |
| 136 | void drawElement(SkCanvas* canvas) override { |
| 137 | SkPaint paint; |
| 138 | paint.setColor(SK_ColorRED); |
| 139 | canvas->drawRect(SkRect::MakeXYWH(1, 1, 3, 3), paint); |
| 140 | } |
| 141 | |
| 142 | private: |
| 143 | typedef PixelSnapGM INHERITED; |
| 144 | }; |
| 145 | |
| 146 | class ComboSnapGM : public PixelSnapGM { |
| 147 | protected: |
| 148 | SkString onShortName() override { return SkString("pixel_snap_combo"); } |
| 149 | void drawElement(SkCanvas* canvas) override { |
| 150 | SkPaint paint; |
| 151 | paint.setAntiAlias(false); |
| 152 | // A rectangle that exactly covers a pixel, a point at each corner, 8 horiz/vert lines |
| 153 | // at rect corners (two at each corner, extending away from rect). They are drawn in this |
| 154 | // order lines (green), points (blue), rect(red). |
| 155 | SkRect rect = SkRect::MakeXYWH(3, 3, 1, 1); |
| 156 | paint.setColor(SK_ColorGREEN); |
| 157 | canvas->drawLine(3, 3, 0, 3, paint); |
| 158 | canvas->drawLine(3, 3, 3, 0, paint); |
| 159 | canvas->drawLine(4, 3, 7, 3, paint); |
| 160 | canvas->drawLine(4, 3, 4, 0, paint); |
| 161 | canvas->drawLine(3, 4, 0, 4, paint); |
| 162 | canvas->drawLine(3, 4, 3, 7, paint); |
| 163 | canvas->drawLine(4, 4, 7, 4, paint); |
| 164 | canvas->drawLine(4, 4, 4, 7, paint); |
| 165 | canvas->drawPoint(4, 3, SK_ColorBLUE); |
| 166 | canvas->drawPoint(4, 4, SK_ColorBLUE); |
| 167 | canvas->drawPoint(3, 3, SK_ColorBLUE); |
| 168 | canvas->drawPoint(3, 4, SK_ColorBLUE); |
| 169 | paint.setColor(SK_ColorRED); |
| 170 | canvas->drawRect(rect, paint); |
| 171 | } |
| 172 | |
| 173 | private: |
| 174 | typedef PixelSnapGM INHERITED; |
| 175 | }; |
| 176 | |
| 177 | ////////////////////////////////////////////////////////////////////////////// |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 178 | DEF_GM(return new PointSnapGM;) |
| 179 | DEF_GM(return new LineSnapGM;) |
| 180 | DEF_GM(return new RectSnapGM;) |
| 181 | DEF_GM(return new ComboSnapGM;) |