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/gm.h" 9 #include "include/core/SkCanvas.h" 10 #include "include/core/SkColor.h" 11 #include "include/core/SkFont.h" 12 #include "include/core/SkPaint.h" 13 #include "include/core/SkPoint.h" 14 #include "include/core/SkRect.h" 15 #include "include/core/SkScalar.h" 16 #include "include/core/SkShader.h" 17 #include "include/core/SkSize.h" 18 #include "include/core/SkString.h" 19 #include "include/core/SkTypeface.h" 20 #include "include/core/SkTypes.h" 21 #include "tools/ToolUtils.h" 22 23 // This class of GMs test how edges/verts snap near rounding boundaries in device space without 24 // anti-aliaing. 25 class PixelSnapGM : public skiagm::GM { 26 public: PixelSnapGM()27 PixelSnapGM() {} 28 29 protected: 30 // kTrans should be even or checkboards wont agree in different test cases. 31 static constexpr int kTrans = 14; 32 static constexpr int kLabelPad = 4; 33 // The inverse of this value should be a perfect SkScalar. 34 static constexpr int kSubPixelSteps = 8; 35 static constexpr int kLabelTextSize = 9; 36 37 static_assert(kSubPixelSteps < 99, "label_offset_too_small"); 38 static constexpr int kLabelOffsetX = 2 * kLabelTextSize + kLabelPad; 39 static constexpr int kLabelOffsetY = kLabelTextSize + kLabelPad; 40 onISize()41 SkISize onISize() override { 42 return SkISize::Make((kSubPixelSteps + 1) * kTrans + kLabelOffsetX + kLabelPad, 43 (kSubPixelSteps + 1) * kTrans + kLabelOffsetY + kLabelPad); 44 } 45 onDraw(SkCanvas * canvas)46 void onDraw(SkCanvas* canvas) override { 47 SkPaint bgPaint; 48 bgPaint.setShader(ToolUtils::create_checkerboard_shader(0xFFAAAAAA, 0xFF777777, 1)); 49 canvas->drawPaint(bgPaint); 50 51 SkString offset; 52 SkPaint labelPaint; 53 labelPaint.setColor(SK_ColorWHITE); 54 SkFont font(ToolUtils::create_portable_typeface(), SkIntToScalar(kLabelTextSize)); 55 SkPaint linePaint; 56 linePaint.setColor(SK_ColorWHITE); 57 58 // Draw row labels 59 canvas->save(); 60 canvas->translate(0, SkIntToScalar(kLabelOffsetY)); 61 for (int i = 0; i <= kSubPixelSteps; ++i) { 62 offset.printf("%d", i); 63 canvas->drawString(offset, 0, i * kTrans + SkIntToScalar(kLabelTextSize), 64 font, labelPaint); 65 } 66 canvas->restore(); 67 68 // Draw col labels 69 canvas->save(); 70 canvas->translate(SkIntToScalar(kLabelOffsetX), 0); 71 for (int i = 0; i <= kSubPixelSteps; ++i) { 72 offset.printf("%d", i); 73 canvas->drawString(offset, i * SkIntToScalar(kTrans), SkIntToScalar(kLabelTextSize), 74 font, labelPaint); 75 } 76 canvas->restore(); 77 78 canvas->translate(SkIntToScalar(kLabelOffsetX), SkIntToScalar(kLabelOffsetY)); 79 80 // Draw test case grid lines (Draw them all at pixel centers to hopefully avoid any 81 // snapping issues). 82 for (int i = 0; i <= kSubPixelSteps + 1; ++i) { 83 canvas->drawLine(0.5f, 84 i * SkIntToScalar(kTrans) + 0.5f, 85 SkIntToScalar(kTrans) * (kSubPixelSteps + 1) + 0.5f, 86 i * SkIntToScalar(kTrans) + 0.5f, 87 linePaint); 88 canvas->drawLine(i * SkIntToScalar(kTrans) + 0.5f, 89 0.5f, 90 i * SkIntToScalar(kTrans) + 0.5f, 91 SkIntToScalar(kTrans) * (kSubPixelSteps + 1) + 0.5f, 92 linePaint); 93 } 94 95 for (int i = 0; i <= kSubPixelSteps; ++i) { 96 for (int j = 0; j <= kSubPixelSteps; ++j) { 97 canvas->save(); 98 // +1's account for the grid lines around each test case. 99 canvas->translate(j * (kTrans + 1.f/kSubPixelSteps) + 1, 100 i * (kTrans + 1.f/kSubPixelSteps) + 1); 101 this->drawElement(canvas); 102 canvas->restore(); 103 } 104 } 105 } 106 107 virtual void drawElement(SkCanvas*) = 0; 108 109 private: 110 typedef skiagm::GM INHERITED; 111 }; 112 113 class PointSnapGM : public PixelSnapGM { 114 protected: onShortName()115 SkString onShortName() override { return SkString("pixel_snap_point"); } drawElement(SkCanvas * canvas)116 void drawElement(SkCanvas* canvas) override { 117 const SkPoint pt = { 1, 1 }; 118 SkPaint paint; 119 paint.setColor(SK_ColorBLUE); 120 canvas->drawPoints(SkCanvas::kPoints_PointMode, 1, &pt, paint); 121 } 122 123 private: 124 typedef PixelSnapGM INHERITED; 125 }; 126 127 class LineSnapGM : public PixelSnapGM { 128 protected: onShortName()129 SkString onShortName() override { return SkString("pixel_snap_line"); } drawElement(SkCanvas * canvas)130 void drawElement(SkCanvas* canvas) override { 131 SkPaint paint; 132 paint.setColor(SK_ColorGREEN); 133 // Draw a horizontal and vertical line, each length 3. 134 canvas->drawLine(1, 1, 4, 1, paint); 135 canvas->drawLine(6, 1, 6, 4, paint); 136 } 137 138 private: 139 typedef PixelSnapGM INHERITED; 140 }; 141 142 class RectSnapGM : public PixelSnapGM { 143 protected: onShortName()144 SkString onShortName() override { return SkString("pixel_snap_rect"); } drawElement(SkCanvas * canvas)145 void drawElement(SkCanvas* canvas) override { 146 SkPaint paint; 147 paint.setColor(SK_ColorRED); 148 canvas->drawRect(SkRect::MakeXYWH(1, 1, 3, 3), paint); 149 } 150 151 private: 152 typedef PixelSnapGM INHERITED; 153 }; 154 155 class ComboSnapGM : public PixelSnapGM { 156 protected: onShortName()157 SkString onShortName() override { return SkString("pixel_snap_combo"); } drawElement(SkCanvas * canvas)158 void drawElement(SkCanvas* canvas) override { 159 SkPaint paint; 160 paint.setAntiAlias(false); 161 // A rectangle that exactly covers a pixel, a point at each corner, 8 horiz/vert lines 162 // at rect corners (two at each corner, extending away from rect). They are drawn in this 163 // order lines (green), points (blue), rect(red). 164 SkRect rect = SkRect::MakeXYWH(3, 3, 1, 1); 165 paint.setColor(SK_ColorGREEN); 166 const SkPoint lines[] = { 167 { 3, 3 }, { 0, 3 }, 168 { 3, 3 }, { 3, 0 }, 169 { 4, 3 }, { 7, 3 }, 170 { 4, 3 }, { 4, 0 }, 171 { 3, 4 }, { 0, 4 }, 172 { 3, 4 }, { 3, 7 }, 173 { 4, 4 }, { 7, 4 }, 174 { 4, 4 }, { 4, 7 }, 175 }; 176 canvas->drawPoints(SkCanvas::kLines_PointMode, SK_ARRAY_COUNT(lines), lines, paint); 177 178 const SkPoint pts[] = { 179 { 4, 3 }, { 4, 4, }, { 3, 3 }, { 3, 4 }, 180 }; 181 paint.setColor(SK_ColorBLUE); 182 canvas->drawPoints(SkCanvas::kPoints_PointMode, SK_ARRAY_COUNT(pts), pts, paint); 183 184 paint.setColor(SK_ColorRED); 185 canvas->drawRect(rect, paint); 186 } 187 188 private: 189 typedef PixelSnapGM INHERITED; 190 }; 191 192 ////////////////////////////////////////////////////////////////////////////// 193 DEF_GM(return new PointSnapGM;) 194 DEF_GM(return new LineSnapGM;) 195 DEF_GM(return new RectSnapGM;) 196 DEF_GM(return new ComboSnapGM;) 197