2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "sk_tool_utils.h"
13 // This class of GMs test how edges/verts snap near rounding boundaries in device space without
15 class PixelSnapGM : public skiagm::GM {
20 // kTrans should be even or checkboards wont agree in different test cases.
21 static constexpr int kTrans = 14;
22 static constexpr int kLabelPad = 4;
23 // The inverse of this value should be a perfect SkScalar.
24 static constexpr int kSubPixelSteps = 8;
25 static constexpr int kLabelTextSize = 9;
27 static_assert(kSubPixelSteps < 99, "label_offset_too_small");
28 static constexpr int kLabelOffsetX = 2 * kLabelTextSize + kLabelPad;
29 static constexpr int kLabelOffsetY = kLabelTextSize + kLabelPad;
31 SkISize onISize() override {
32 return SkISize::Make((kSubPixelSteps + 1) * kTrans + kLabelOffsetX + kLabelPad,
33 (kSubPixelSteps + 1) * kTrans + kLabelOffsetY + kLabelPad);
36 void onDraw(SkCanvas* canvas) override {
39 sk_tool_utils::create_checkerboard_shader(
40 sk_tool_utils::color_to_565(0xFFAAAAAA),
41 sk_tool_utils::color_to_565(0xFF777777), 1));
42 canvas->drawPaint(bgPaint);
46 labelPaint.setAntiAlias(true);
47 labelPaint.setColor(SK_ColorWHITE);
48 labelPaint.setTextSize(SkIntToScalar(kLabelTextSize));
49 sk_tool_utils::set_portable_typeface(&labelPaint);
51 linePaint.setColor(SK_ColorWHITE);
55 canvas->translate(0, SkIntToScalar(kLabelOffsetY));
56 for (int i = 0; i <= kSubPixelSteps; ++i) {
57 offset.printf("%d", i);
58 canvas->drawString(offset,
59 0, i * kTrans + labelPaint.getTextSize(),
66 canvas->translate(SkIntToScalar(kLabelOffsetX), 0);
67 for (int i = 0; i <= kSubPixelSteps; ++i) {
68 offset.printf("%d", i);
69 canvas->drawString(offset,
70 i * SkIntToScalar(kTrans), labelPaint.getTextSize(),
75 canvas->translate(SkIntToScalar(kLabelOffsetX), SkIntToScalar(kLabelOffsetY));
77 // Draw test case grid lines (Draw them all at pixel centers to hopefully avoid any
79 for (int i = 0; i <= kSubPixelSteps + 1; ++i) {
80 canvas->drawLine(0.5f,
81 i * SkIntToScalar(kTrans) + 0.5f,
82 SkIntToScalar(kTrans) * (kSubPixelSteps + 1) + 0.5f,
83 i * SkIntToScalar(kTrans) + 0.5f,
85 canvas->drawLine(i * SkIntToScalar(kTrans) + 0.5f,
87 i * SkIntToScalar(kTrans) + 0.5f,
88 SkIntToScalar(kTrans) * (kSubPixelSteps + 1) + 0.5f,
92 for (int i = 0; i <= kSubPixelSteps; ++i) {
93 for (int j = 0; j <= kSubPixelSteps; ++j) {
95 // +1's account for the grid lines around each test case.
96 canvas->translate(j * (kTrans + 1.f/kSubPixelSteps) + 1,
97 i * (kTrans + 1.f/kSubPixelSteps) + 1);
98 this->drawElement(canvas);
104 virtual void drawElement(SkCanvas*) = 0;
107 typedef skiagm::GM INHERITED;
110 class PointSnapGM : public PixelSnapGM {
112 SkString onShortName() override { return SkString("pixel_snap_point"); }
113 void drawElement(SkCanvas* canvas) override {
114 const SkPoint pt = { 1, 1 };
116 paint.setColor(SK_ColorBLUE);
117 canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, &pt, paint);
121 typedef PixelSnapGM INHERITED;
124 class LineSnapGM : public PixelSnapGM {
126 SkString onShortName() override { return SkString("pixel_snap_line"); }
127 void drawElement(SkCanvas* canvas) override {
129 paint.setColor(SK_ColorGREEN);
130 // Draw a horizontal and vertical line, each length 3.
131 canvas->drawLine(1, 1, 4, 1, paint);
132 canvas->drawLine(6, 1, 6, 4, paint);
136 typedef PixelSnapGM INHERITED;
139 class RectSnapGM : public PixelSnapGM {
141 SkString onShortName() override { return SkString("pixel_snap_rect"); }
142 void drawElement(SkCanvas* canvas) override {
144 paint.setColor(SK_ColorRED);
145 canvas->drawRect(SkRect::MakeXYWH(1, 1, 3, 3), paint);
149 typedef PixelSnapGM INHERITED;
152 class ComboSnapGM : public PixelSnapGM {
154 SkString onShortName() override { return SkString("pixel_snap_combo"); }
155 void drawElement(SkCanvas* canvas) override {
157 paint.setAntiAlias(false);
158 // A rectangle that exactly covers a pixel, a point at each corner, 8 horiz/vert lines
159 // at rect corners (two at each corner, extending away from rect). They are drawn in this
160 // order lines (green), points (blue), rect(red).
161 SkRect rect = SkRect::MakeXYWH(3, 3, 1, 1);
162 paint.setColor(SK_ColorGREEN);
163 const SkPoint lines[] = {
173 canvas->drawPoints(SkCanvas::kLines_PointMode, SK_ARRAY_COUNT(lines), lines, paint);
175 const SkPoint pts[] = {
176 { 4, 3 }, { 4, 4, }, { 3, 3 }, { 3, 4 },
178 paint.setColor(SK_ColorBLUE);
179 canvas->drawPoints(SkCanvas::kPoints_PointMode, SK_ARRAY_COUNT(pts), pts, paint);
181 paint.setColor(SK_ColorRED);
182 canvas->drawRect(rect, paint);
186 typedef PixelSnapGM INHERITED;
189 //////////////////////////////////////////////////////////////////////////////
190 DEF_GM(return new PointSnapGM;)
191 DEF_GM(return new LineSnapGM;)
192 DEF_GM(return new RectSnapGM;)
193 DEF_GM(return new ComboSnapGM;)