blob: f4e45827c179bcbb8860019e518ec8f47b0883e7 [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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
bsalomon87cbcf32015-04-22 08:51:38 -070010
11#include "SkShader.h"
12
13// This class of GMs test how edges/verts snap near rounding boundaries in device space without
14// anti-aliaing.
15class PixelSnapGM : public skiagm::GM {
16public:
17 PixelSnapGM() {}
18
19protected:
20 // kTrans should be even or checkboards wont agree in different test cases.
mtkleindbfd7ab2016-09-01 11:24:54 -070021 static constexpr int kTrans = 14;
22 static constexpr int kLabelPad = 4;
bsalomon87cbcf32015-04-22 08:51:38 -070023 // The inverse of this value should be a perfect SkScalar.
mtkleindbfd7ab2016-09-01 11:24:54 -070024 static constexpr int kSubPixelSteps = 8;
25 static constexpr int kLabelTextSize = 9;
bsalomon87cbcf32015-04-22 08:51:38 -070026
bungeman99fe8222015-08-20 07:57:51 -070027 static_assert(kSubPixelSteps < 99, "label_offset_too_small");
mtkleindbfd7ab2016-09-01 11:24:54 -070028 static constexpr int kLabelOffsetX = 2 * kLabelTextSize + kLabelPad;
29 static constexpr int kLabelOffsetY = kLabelTextSize + kLabelPad;
bsalomonc6534a22015-04-22 11:34:46 -070030
bsalomon87cbcf32015-04-22 08:51:38 -070031 SkISize onISize() override {
bsalomonc6534a22015-04-22 11:34:46 -070032 return SkISize::Make((kSubPixelSteps + 1) * kTrans + kLabelOffsetX + kLabelPad,
33 (kSubPixelSteps + 1) * kTrans + kLabelOffsetY + kLabelPad);
bsalomon87cbcf32015-04-22 08:51:38 -070034 }
35
36 void onDraw(SkCanvas* canvas) override {
37 SkPaint bgPaint;
38 bgPaint.setShader(
caryclarkf597c422015-07-28 10:37:53 -070039 sk_tool_utils::create_checkerboard_shader(
halcanary9d524f22016-03-29 09:03:52 -070040 sk_tool_utils::color_to_565(0xFFAAAAAA),
reed8a21c9f2016-03-08 18:50:00 -080041 sk_tool_utils::color_to_565(0xFF777777), 1));
bsalomon87cbcf32015-04-22 08:51:38 -070042 canvas->drawPaint(bgPaint);
43
44 SkString offset;
45 SkPaint labelPaint;
46 labelPaint.setAntiAlias(true);
47 labelPaint.setColor(SK_ColorWHITE);
48 labelPaint.setTextSize(SkIntToScalar(kLabelTextSize));
caryclarkf597c422015-07-28 10:37:53 -070049 sk_tool_utils::set_portable_typeface(&labelPaint);
bsalomon87cbcf32015-04-22 08:51:38 -070050 SkPaint linePaint;
51 linePaint.setColor(SK_ColorWHITE);
52
caryclarkf597c422015-07-28 10:37:53 -070053 // Draw row labels
54 canvas->save();
55 canvas->translate(0, SkIntToScalar(kLabelOffsetY));
56 for (int i = 0; i <= kSubPixelSteps; ++i) {
57 offset.printf("%d", i);
Cary Clark2a475ea2017-04-28 15:35:12 -040058 canvas->drawString(offset,
caryclarkf597c422015-07-28 10:37:53 -070059 0, i * kTrans + labelPaint.getTextSize(),
60 labelPaint);
61 }
62 canvas->restore();
bsalomon87cbcf32015-04-22 08:51:38 -070063
caryclarkf597c422015-07-28 10:37:53 -070064 // Draw col labels
65 canvas->save();
66 canvas->translate(SkIntToScalar(kLabelOffsetX), 0);
67 for (int i = 0; i <= kSubPixelSteps; ++i) {
68 offset.printf("%d", i);
Cary Clark2a475ea2017-04-28 15:35:12 -040069 canvas->drawString(offset,
caryclarkf597c422015-07-28 10:37:53 -070070 i * SkIntToScalar(kTrans), labelPaint.getTextSize(),
71 labelPaint);
72 }
73 canvas->restore();
bsalomon87cbcf32015-04-22 08:51:38 -070074
bsalomonc6534a22015-04-22 11:34:46 -070075 canvas->translate(SkIntToScalar(kLabelOffsetX), SkIntToScalar(kLabelOffsetY));
bsalomon87cbcf32015-04-22 08:51:38 -070076
77 // Draw test case grid lines (Draw them all at pixel centers to hopefully avoid any
78 // snapping issues).
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,
84 linePaint);
85 canvas->drawLine(i * SkIntToScalar(kTrans) + 0.5f,
86 0.5f,
87 i * SkIntToScalar(kTrans) + 0.5f,
88 SkIntToScalar(kTrans) * (kSubPixelSteps + 1) + 0.5f,
89 linePaint);
90 }
91
92 for (int i = 0; i <= kSubPixelSteps; ++i) {
93 for (int j = 0; j <= kSubPixelSteps; ++j) {
94 canvas->save();
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);
99 canvas->restore();
100 }
101 }
102 }
103
104 virtual void drawElement(SkCanvas*) = 0;
105
106private:
107 typedef skiagm::GM INHERITED;
108};
109
110class PointSnapGM : public PixelSnapGM {
111protected:
112 SkString onShortName() override { return SkString("pixel_snap_point"); }
Mike Reed3661bc92017-02-22 13:21:42 -0500113 void drawElement(SkCanvas* canvas) override {
114 const SkPoint pt = { 1, 1 };
115 SkPaint paint;
116 paint.setColor(SK_ColorBLUE);
117 canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, &pt, paint);
118 }
bsalomon87cbcf32015-04-22 08:51:38 -0700119
120private:
121 typedef PixelSnapGM INHERITED;
122};
123
124class LineSnapGM : public PixelSnapGM {
125protected:
126 SkString onShortName() override { return SkString("pixel_snap_line"); }
127 void drawElement(SkCanvas* canvas) override {
128 SkPaint paint;
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);
133 }
134
135private:
136 typedef PixelSnapGM INHERITED;
137};
138
139class RectSnapGM : public PixelSnapGM {
140protected:
141 SkString onShortName() override { return SkString("pixel_snap_rect"); }
142 void drawElement(SkCanvas* canvas) override {
143 SkPaint paint;
144 paint.setColor(SK_ColorRED);
145 canvas->drawRect(SkRect::MakeXYWH(1, 1, 3, 3), paint);
146 }
147
148private:
149 typedef PixelSnapGM INHERITED;
150};
151
152class ComboSnapGM : public PixelSnapGM {
153protected:
154 SkString onShortName() override { return SkString("pixel_snap_combo"); }
155 void drawElement(SkCanvas* canvas) override {
156 SkPaint paint;
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);
Mike Reed3661bc92017-02-22 13:21:42 -0500163 const SkPoint lines[] = {
164 { 3, 3 }, { 0, 3 },
165 { 3, 3 }, { 3, 0 },
166 { 4, 3 }, { 7, 3 },
167 { 4, 3 }, { 4, 0 },
168 { 3, 4 }, { 0, 4 },
169 { 3, 4 }, { 3, 7 },
170 { 4, 4 }, { 7, 4 },
171 { 4, 4 }, { 4, 7 },
172 };
173 canvas->drawPoints(SkCanvas::kLines_PointMode, SK_ARRAY_COUNT(lines), lines, paint);
174
175 const SkPoint pts[] = {
176 { 4, 3 }, { 4, 4, }, { 3, 3 }, { 3, 4 },
177 };
178 paint.setColor(SK_ColorBLUE);
179 canvas->drawPoints(SkCanvas::kPoints_PointMode, SK_ARRAY_COUNT(pts), pts, paint);
180
bsalomon87cbcf32015-04-22 08:51:38 -0700181 paint.setColor(SK_ColorRED);
182 canvas->drawRect(rect, paint);
183 }
184
185private:
186 typedef PixelSnapGM INHERITED;
187};
188
189//////////////////////////////////////////////////////////////////////////////
halcanary385fe4d2015-08-26 13:07:48 -0700190DEF_GM(return new PointSnapGM;)
191DEF_GM(return new LineSnapGM;)
192DEF_GM(return new RectSnapGM;)
193DEF_GM(return new ComboSnapGM;)