Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(538)

Side by Side Diff: gm/pixelsnap.cpp

Issue 1070213003: Add GM to test non-AA pixel snapping for points, lines, rects. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: remove SkShader.h Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | gyp/gmslides.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 // This class of GMs test how edges/verts snap near rounding boundaries in devic e space without
11 // anti-aliaing.
12 class PixelSnapGM : public skiagm::GM {
13 public:
14 PixelSnapGM() {}
15
16 protected:
17 // kTrans should be even or checkboards wont agree in different test cases.
18 static const int kTrans = 14;
19 static const int kLabelPad = 4;
20 // The inverse of this value should be a perfect SkScalar.
21 static const int kSubPixelSteps = 8;
22 static const int kLabelTextSize = 9;
23
24 SkISize onISize() override {
25 SkPaint labelPaint;
26 labelPaint.setAntiAlias(true);
27 labelPaint.setColor(SK_ColorWHITE);
28 labelPaint.setTextSize(SkIntToScalar(kLabelTextSize));
29 // Assert that we only render double digit labels
30 SkASSERT(kSubPixelSteps < 99);
31 // Pick 88 as widest possible label rendered (?)
32 int xoffset = SkScalarCeilToInt(labelPaint.measureText("88", 2));
33 int yoffset = SkScalarCeilToInt(labelPaint.getTextSize());
robertphillips 2015/04/22 15:28:11 "+( " -> "+ (" ?
bsalomon 2015/04/22 15:46:02 Done.
34 return SkISize::Make(kLabelPad +( kSubPixelSteps + 1) * kTrans + xoffset ,
35 kLabelPad +( kSubPixelSteps + 1) * kTrans + yoffset );
36 }
37
38 void onDraw(SkCanvas* canvas) override {
39 SkPaint bgPaint;
40 bgPaint.setShader(
41 sk_tool_utils::create_checkerboard_shader(0xFFAAAAAA, 0xFF777777, 1) )->unref();
42 canvas->drawPaint(bgPaint);
43
44 SkString offset;
45 SkPaint labelPaint;
46 labelPaint.setAntiAlias(true);
47 labelPaint.setColor(SK_ColorWHITE);
48 labelPaint.setTextSize(SkIntToScalar(kLabelTextSize));
49 SkPaint linePaint;
50 linePaint.setColor(SK_ColorWHITE);
51
52 // Draw row labels
53 static const SkScalar labelOffsetY = labelPaint.getTextSize() + kLabelPa d;
54 SkScalar labelOffsetX = 0;
55 canvas->save();
56 canvas->translate(0, labelOffsetY);
57 for (int i = 0; i <= kSubPixelSteps; ++i) {
58 offset.printf("%d", i);
59 canvas->drawText(offset.c_str(), offset.size(),
60 0, i * kTrans + labelPaint.getTextSize(),
61 labelPaint);
62 labelOffsetX = SkTMax(labelPaint.measureText(offset.c_str(), off set.size()),
63 labelOffsetX);
64 }
65 canvas->restore();
66 labelOffsetX += kLabelPad;
67 labelOffsetX = SkScalarCeilToScalar(labelOffsetX);
68
69 // Draw col labels
70 canvas->save();
71 canvas->translate(labelOffsetX, 0);
72 for (int i = 0; i <= kSubPixelSteps; ++i) {
73 offset.printf("%d", i);
74 canvas->drawText(offset.c_str(), offset.size(),
75 i * SkIntToScalar(kTrans), labelPaint.getTextSi ze(),
76 labelPaint);
77 }
78 canvas->restore();
79
80 canvas->translate(labelOffsetX, labelOffsetY);
81 SkASSERT((SkScalar)(int)labelOffsetX == labelOffsetX);
82 SkASSERT((SkScalar)(int)labelOffsetY == labelOffsetY);
83
84 // Draw test case grid lines (Draw them all at pixel centers to hopefull y avoid any
85 // snapping issues).
86 for (int i = 0; i <= kSubPixelSteps + 1; ++i) {
87 canvas->drawLine(0.5f,
88 i * SkIntToScalar(kTrans) + 0.5f,
89 SkIntToScalar(kTrans) * (kSubPixelSteps + 1) + 0.5f ,
90 i * SkIntToScalar(kTrans) + 0.5f,
91 linePaint);
92 canvas->drawLine(i * SkIntToScalar(kTrans) + 0.5f,
93 0.5f,
94 i * SkIntToScalar(kTrans) + 0.5f,
95 SkIntToScalar(kTrans) * (kSubPixelSteps + 1) + 0.5f ,
96 linePaint);
97 }
98
99 for (int i = 0; i <= kSubPixelSteps; ++i) {
100 for (int j = 0; j <= kSubPixelSteps; ++j) {
101 canvas->save();
102 // +1's account for the grid lines around each test case.
103 canvas->translate(j * (kTrans + 1.f/kSubPixelSteps) + 1,
104 i * (kTrans + 1.f/kSubPixelSteps) + 1);
105 this->drawElement(canvas);
106 canvas->restore();
107 }
108 }
109 }
110
111 virtual void drawElement(SkCanvas*) = 0;
112
113 private:
robertphillips 2015/04/22 15:28:11 skiagm::GM ?
bsalomon 2015/04/22 15:46:02 Done.
114 typedef PixelSnapGM INHERITED;
115 };
116
117 class PointSnapGM : public PixelSnapGM {
118 protected:
119 SkString onShortName() override { return SkString("pixel_snap_point"); }
120 void drawElement(SkCanvas* canvas) override { canvas->drawPoint(1, 1, SK_Col orBLUE); }
121
robertphillips 2015/04/22 15:28:11 private: PixelSnapGM ?
bsalomon 2015/04/22 15:46:02 Done.
122 typedef skiagm::GM INHERITED;
123 };
124
125 class LineSnapGM : public PixelSnapGM {
126 protected:
127 SkString onShortName() override { return SkString("pixel_snap_line"); }
128 void drawElement(SkCanvas* canvas) override {
129 SkPaint paint;
130 paint.setColor(SK_ColorGREEN);
131 // Draw a horizontal and vertical line, each length 3.
132 canvas->drawLine(1, 1, 4, 1, paint);
133 canvas->drawLine(6, 1, 6, 4, paint);
134 }
135
136 private:
137 typedef PixelSnapGM INHERITED;
138 };
139
140 class RectSnapGM : public PixelSnapGM {
141 protected:
142 SkString onShortName() override { return SkString("pixel_snap_rect"); }
143 void drawElement(SkCanvas* canvas) override {
144 SkPaint paint;
145 paint.setColor(SK_ColorRED);
146 canvas->drawRect(SkRect::MakeXYWH(1, 1, 3, 3), paint);
147 }
148
149 private:
150 typedef PixelSnapGM INHERITED;
151 };
152
153 class ComboSnapGM : public PixelSnapGM {
154 protected:
155 SkString onShortName() override { return SkString("pixel_snap_combo"); }
156 void drawElement(SkCanvas* canvas) override {
157 SkPaint paint;
158 paint.setAntiAlias(false);
159 // A rectangle that exactly covers a pixel, a point at each corner, 8 ho riz/vert lines
160 // at rect corners (two at each corner, extending away from rect). They are drawn in this
161 // order lines (green), points (blue), rect(red).
162 SkRect rect = SkRect::MakeXYWH(3, 3, 1, 1);
163 paint.setColor(SK_ColorGREEN);
164 canvas->drawLine(3, 3, 0, 3, paint);
165 canvas->drawLine(3, 3, 3, 0, paint);
166 canvas->drawLine(4, 3, 7, 3, paint);
167 canvas->drawLine(4, 3, 4, 0, paint);
168 canvas->drawLine(3, 4, 0, 4, paint);
169 canvas->drawLine(3, 4, 3, 7, paint);
170 canvas->drawLine(4, 4, 7, 4, paint);
171 canvas->drawLine(4, 4, 4, 7, paint);
172 canvas->drawPoint(4, 3, SK_ColorBLUE);
173 canvas->drawPoint(4, 4, SK_ColorBLUE);
174 canvas->drawPoint(3, 3, SK_ColorBLUE);
175 canvas->drawPoint(3, 4, SK_ColorBLUE);
176 paint.setColor(SK_ColorRED);
177 canvas->drawRect(rect, paint);
178 }
179
180 private:
181 typedef PixelSnapGM INHERITED;
182 };
183
184 //////////////////////////////////////////////////////////////////////////////
185 DEF_GM( return SkNEW(PointSnapGM); )
186 DEF_GM( return SkNEW(LineSnapGM); )
187 DEF_GM( return SkNEW(RectSnapGM); )
188 DEF_GM( return SkNEW(ComboSnapGM); )
OLDNEW
« no previous file with comments | « no previous file | gyp/gmslides.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698