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