blob: 18fb7b2b7522aefe1b39ff3c9f05287340740654 [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"); }
112 void drawElement(SkCanvas* canvas) override { canvas->drawPoint(1, 1, SK_ColorBLUE); }
113
114private:
115 typedef PixelSnapGM INHERITED;
116};
117
118class LineSnapGM : public PixelSnapGM {
119protected:
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
129private:
130 typedef PixelSnapGM INHERITED;
131};
132
133class RectSnapGM : public PixelSnapGM {
134protected:
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
142private:
143 typedef PixelSnapGM INHERITED;
144};
145
146class ComboSnapGM : public PixelSnapGM {
147protected:
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
173private:
174 typedef PixelSnapGM INHERITED;
175};
176
177//////////////////////////////////////////////////////////////////////////////
halcanary385fe4d2015-08-26 13:07:48 -0700178DEF_GM(return new PointSnapGM;)
179DEF_GM(return new LineSnapGM;)
180DEF_GM(return new RectSnapGM;)
181DEF_GM(return new ComboSnapGM;)