blob: 401127d46269447e10896a8e31e1b81bd2d889dc [file] [log] [blame]
bsalomon87cbcf32015-04-22 08:51:38 -07001/*
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.
14class PixelSnapGM : public skiagm::GM {
15public:
16 PixelSnapGM() {}
17
18protected:
19 // kTrans should be even or checkboards wont agree in different test cases.
mtkleindbfd7ab2016-09-01 11:24:54 -070020 static constexpr int kTrans = 14;
21 static constexpr int kLabelPad = 4;
bsalomon87cbcf32015-04-22 08:51:38 -070022 // The inverse of this value should be a perfect SkScalar.
mtkleindbfd7ab2016-09-01 11:24:54 -070023 static constexpr int kSubPixelSteps = 8;
24 static constexpr int kLabelTextSize = 9;
bsalomon87cbcf32015-04-22 08:51:38 -070025
bungeman99fe8222015-08-20 07:57:51 -070026 static_assert(kSubPixelSteps < 99, "label_offset_too_small");
mtkleindbfd7ab2016-09-01 11:24:54 -070027 static constexpr int kLabelOffsetX = 2 * kLabelTextSize + kLabelPad;
28 static constexpr int kLabelOffsetY = kLabelTextSize + kLabelPad;
bsalomonc6534a22015-04-22 11:34:46 -070029
bsalomon87cbcf32015-04-22 08:51:38 -070030 SkISize onISize() override {
bsalomonc6534a22015-04-22 11:34:46 -070031 return SkISize::Make((kSubPixelSteps + 1) * kTrans + kLabelOffsetX + kLabelPad,
32 (kSubPixelSteps + 1) * kTrans + kLabelOffsetY + kLabelPad);
bsalomon87cbcf32015-04-22 08:51:38 -070033 }
34
35 void onDraw(SkCanvas* canvas) override {
36 SkPaint bgPaint;
37 bgPaint.setShader(
caryclarkf597c422015-07-28 10:37:53 -070038 sk_tool_utils::create_checkerboard_shader(
halcanary9d524f22016-03-29 09:03:52 -070039 sk_tool_utils::color_to_565(0xFFAAAAAA),
reed8a21c9f2016-03-08 18:50:00 -080040 sk_tool_utils::color_to_565(0xFF777777), 1));
bsalomon87cbcf32015-04-22 08:51:38 -070041 canvas->drawPaint(bgPaint);
42
43 SkString offset;
44 SkPaint labelPaint;
45 labelPaint.setAntiAlias(true);
46 labelPaint.setColor(SK_ColorWHITE);
47 labelPaint.setTextSize(SkIntToScalar(kLabelTextSize));
caryclarkf597c422015-07-28 10:37:53 -070048 sk_tool_utils::set_portable_typeface(&labelPaint);
bsalomon87cbcf32015-04-22 08:51:38 -070049 SkPaint linePaint;
50 linePaint.setColor(SK_ColorWHITE);
51
caryclarkf597c422015-07-28 10:37:53 -070052 // 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();
bsalomon87cbcf32015-04-22 08:51:38 -070062
caryclarkf597c422015-07-28 10:37:53 -070063 // 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();
bsalomon87cbcf32015-04-22 08:51:38 -070073
bsalomonc6534a22015-04-22 11:34:46 -070074 canvas->translate(SkIntToScalar(kLabelOffsetX), SkIntToScalar(kLabelOffsetY));
bsalomon87cbcf32015-04-22 08:51:38 -070075
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
105private:
106 typedef skiagm::GM INHERITED;
107};
108
109class PointSnapGM : public PixelSnapGM {
110protected:
111 SkString onShortName() override { return SkString("pixel_snap_point"); }
Mike Reed3661bc92017-02-22 13:21:42 -0500112 void drawElement(SkCanvas* canvas) override {
113 const SkPoint pt = { 1, 1 };
114 SkPaint paint;
115 paint.setColor(SK_ColorBLUE);
116 canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, &pt, paint);
117 }
bsalomon87cbcf32015-04-22 08:51:38 -0700118
119private:
120 typedef PixelSnapGM INHERITED;
121};
122
123class LineSnapGM : public PixelSnapGM {
124protected:
125 SkString onShortName() override { return SkString("pixel_snap_line"); }
126 void drawElement(SkCanvas* canvas) override {
127 SkPaint paint;
128 paint.setColor(SK_ColorGREEN);
129 // Draw a horizontal and vertical line, each length 3.
130 canvas->drawLine(1, 1, 4, 1, paint);
131 canvas->drawLine(6, 1, 6, 4, paint);
132 }
133
134private:
135 typedef PixelSnapGM INHERITED;
136};
137
138class RectSnapGM : public PixelSnapGM {
139protected:
140 SkString onShortName() override { return SkString("pixel_snap_rect"); }
141 void drawElement(SkCanvas* canvas) override {
142 SkPaint paint;
143 paint.setColor(SK_ColorRED);
144 canvas->drawRect(SkRect::MakeXYWH(1, 1, 3, 3), paint);
145 }
146
147private:
148 typedef PixelSnapGM INHERITED;
149};
150
151class ComboSnapGM : public PixelSnapGM {
152protected:
153 SkString onShortName() override { return SkString("pixel_snap_combo"); }
154 void drawElement(SkCanvas* canvas) override {
155 SkPaint paint;
156 paint.setAntiAlias(false);
157 // A rectangle that exactly covers a pixel, a point at each corner, 8 horiz/vert lines
158 // at rect corners (two at each corner, extending away from rect). They are drawn in this
159 // order lines (green), points (blue), rect(red).
160 SkRect rect = SkRect::MakeXYWH(3, 3, 1, 1);
161 paint.setColor(SK_ColorGREEN);
Mike Reed3661bc92017-02-22 13:21:42 -0500162 const SkPoint lines[] = {
163 { 3, 3 }, { 0, 3 },
164 { 3, 3 }, { 3, 0 },
165 { 4, 3 }, { 7, 3 },
166 { 4, 3 }, { 4, 0 },
167 { 3, 4 }, { 0, 4 },
168 { 3, 4 }, { 3, 7 },
169 { 4, 4 }, { 7, 4 },
170 { 4, 4 }, { 4, 7 },
171 };
172 canvas->drawPoints(SkCanvas::kLines_PointMode, SK_ARRAY_COUNT(lines), lines, paint);
173
174 const SkPoint pts[] = {
175 { 4, 3 }, { 4, 4, }, { 3, 3 }, { 3, 4 },
176 };
177 paint.setColor(SK_ColorBLUE);
178 canvas->drawPoints(SkCanvas::kPoints_PointMode, SK_ARRAY_COUNT(pts), pts, paint);
179
bsalomon87cbcf32015-04-22 08:51:38 -0700180 paint.setColor(SK_ColorRED);
181 canvas->drawRect(rect, paint);
182 }
183
184private:
185 typedef PixelSnapGM INHERITED;
186};
187
188//////////////////////////////////////////////////////////////////////////////
halcanary385fe4d2015-08-26 13:07:48 -0700189DEF_GM(return new PointSnapGM;)
190DEF_GM(return new LineSnapGM;)
191DEF_GM(return new RectSnapGM;)
192DEF_GM(return new ComboSnapGM;)