| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "gm.h" | 8 #include "gm.h" |
| 9 | 9 |
| 10 #include "SkShader.h" | 10 #include "SkShader.h" |
| 11 | 11 |
| 12 // This class of GMs test how edges/verts snap near rounding boundaries in devic
e space without | 12 // This class of GMs test how edges/verts snap near rounding boundaries in devic
e space without |
| 13 // anti-aliaing. | 13 // anti-aliaing. |
| 14 class PixelSnapGM : public skiagm::GM { | 14 class PixelSnapGM : public skiagm::GM { |
| 15 public: | 15 public: |
| 16 PixelSnapGM() {} | 16 PixelSnapGM() {} |
| 17 | 17 |
| 18 protected: | 18 protected: |
| 19 // kTrans should be even or checkboards wont agree in different test cases. | 19 // kTrans should be even or checkboards wont agree in different test cases. |
| 20 static const int kTrans = 14; | 20 static const int kTrans = 14; |
| 21 static const int kLabelPad = 4; | 21 static const int kLabelPad = 4; |
| 22 // The inverse of this value should be a perfect SkScalar. | 22 // The inverse of this value should be a perfect SkScalar. |
| 23 static const int kSubPixelSteps = 8; | 23 static const int kSubPixelSteps = 8; |
| 24 static const int kLabelTextSize = 9; | 24 static const int kLabelTextSize = 9; |
| 25 | 25 |
| 26 SK_COMPILE_ASSERT(kSubPixelSteps < 99, label_offset_too_small); |
| 27 static const int kLabelOffsetX = 2 * kLabelTextSize + kLabelPad; |
| 28 static const int kLabelOffsetY = kLabelTextSize + kLabelPad; |
| 29 |
| 26 SkISize onISize() override { | 30 SkISize onISize() override { |
| 27 SkPaint labelPaint; | 31 return SkISize::Make((kSubPixelSteps + 1) * kTrans + kLabelOffsetX + kLa
belPad, |
| 28 labelPaint.setAntiAlias(true); | 32 (kSubPixelSteps + 1) * kTrans + kLabelOffsetY + kLa
belPad); |
| 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 } | 33 } |
| 39 | 34 |
| 40 void onDraw(SkCanvas* canvas) override { | 35 void onDraw(SkCanvas* canvas) override { |
| 41 SkPaint bgPaint; | 36 SkPaint bgPaint; |
| 42 bgPaint.setShader( | 37 bgPaint.setShader( |
| 43 sk_tool_utils::create_checkerboard_shader(0xFFAAAAAA, 0xFF777777, 1)
)->unref(); | 38 sk_tool_utils::create_checkerboard_shader(0xFFAAAAAA, 0xFF777777, 1)
)->unref(); |
| 44 canvas->drawPaint(bgPaint); | 39 canvas->drawPaint(bgPaint); |
| 45 | 40 |
| 46 SkString offset; | 41 SkString offset; |
| 47 SkPaint labelPaint; | 42 SkPaint labelPaint; |
| 48 labelPaint.setAntiAlias(true); | 43 labelPaint.setAntiAlias(true); |
| 49 labelPaint.setColor(SK_ColorWHITE); | 44 labelPaint.setColor(SK_ColorWHITE); |
| 50 labelPaint.setTextSize(SkIntToScalar(kLabelTextSize)); | 45 labelPaint.setTextSize(SkIntToScalar(kLabelTextSize)); |
| 51 SkPaint linePaint; | 46 SkPaint linePaint; |
| 52 linePaint.setColor(SK_ColorWHITE); | 47 linePaint.setColor(SK_ColorWHITE); |
| 53 | 48 |
| 54 // Draw row labels | 49 // Drawing labels is useful for debugging, but bad for baselining (x-pla
tform txt diffs). |
| 55 static const SkScalar labelOffsetY = labelPaint.getTextSize() + kLabelPa
d; | 50 if (false) { |
| 56 SkScalar labelOffsetX = 0; | 51 // Draw row labels |
| 57 canvas->save(); | 52 canvas->save(); |
| 58 canvas->translate(0, labelOffsetY); | 53 canvas->translate(0, SkIntToScalar(kLabelOffsetY)); |
| 59 for (int i = 0; i <= kSubPixelSteps; ++i) { | 54 for (int i = 0; i <= kSubPixelSteps; ++i) { |
| 60 offset.printf("%d", i); | 55 offset.printf("%d", i); |
| 61 canvas->drawText(offset.c_str(), offset.size(), | 56 canvas->drawText(offset.c_str(), offset.size(), |
| 62 0, i * kTrans + labelPaint.getTextSize(), | 57 0, i * kTrans + labelPaint.getTextSize(), |
| 63 labelPaint); | 58 labelPaint); |
| 64 labelOffsetX = SkTMax(labelPaint.measureText(offset.c_str(), off
set.size()), | 59 } |
| 65 labelOffsetX); | 60 canvas->restore(); |
| 66 } | |
| 67 canvas->restore(); | |
| 68 labelOffsetX += kLabelPad; | |
| 69 labelOffsetX = SkScalarCeilToScalar(labelOffsetX); | |
| 70 | 61 |
| 71 // Draw col labels | 62 // Draw col labels |
| 72 canvas->save(); | 63 canvas->save(); |
| 73 canvas->translate(labelOffsetX, 0); | 64 canvas->translate(SkIntToScalar(kLabelOffsetX), 0); |
| 74 for (int i = 0; i <= kSubPixelSteps; ++i) { | 65 for (int i = 0; i <= kSubPixelSteps; ++i) { |
| 75 offset.printf("%d", i); | 66 offset.printf("%d", i); |
| 76 canvas->drawText(offset.c_str(), offset.size(), | 67 canvas->drawText(offset.c_str(), offset.size(), |
| 77 i * SkIntToScalar(kTrans), labelPaint.getTextSi
ze(), | 68 i * SkIntToScalar(kTrans), labelPaint.getTe
xtSize(), |
| 78 labelPaint); | 69 labelPaint); |
| 79 } | 70 } |
| 80 canvas->restore(); | 71 canvas->restore(); |
| 72 } |
| 81 | 73 |
| 82 canvas->translate(labelOffsetX, labelOffsetY); | 74 canvas->translate(SkIntToScalar(kLabelOffsetX), SkIntToScalar(kLabelOffs
etY)); |
| 83 SkASSERT((SkScalar)(int)labelOffsetX == labelOffsetX); | |
| 84 SkASSERT((SkScalar)(int)labelOffsetY == labelOffsetY); | |
| 85 | 75 |
| 86 // Draw test case grid lines (Draw them all at pixel centers to hopefull
y avoid any | 76 // Draw test case grid lines (Draw them all at pixel centers to hopefull
y avoid any |
| 87 // snapping issues). | 77 // snapping issues). |
| 88 for (int i = 0; i <= kSubPixelSteps + 1; ++i) { | 78 for (int i = 0; i <= kSubPixelSteps + 1; ++i) { |
| 89 canvas->drawLine(0.5f, | 79 canvas->drawLine(0.5f, |
| 90 i * SkIntToScalar(kTrans) + 0.5f, | 80 i * SkIntToScalar(kTrans) + 0.5f, |
| 91 SkIntToScalar(kTrans) * (kSubPixelSteps + 1) + 0.5f
, | 81 SkIntToScalar(kTrans) * (kSubPixelSteps + 1) + 0.5f
, |
| 92 i * SkIntToScalar(kTrans) + 0.5f, | 82 i * SkIntToScalar(kTrans) + 0.5f, |
| 93 linePaint); | 83 linePaint); |
| 94 canvas->drawLine(i * SkIntToScalar(kTrans) + 0.5f, | 84 canvas->drawLine(i * SkIntToScalar(kTrans) + 0.5f, |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 | 172 |
| 183 private: | 173 private: |
| 184 typedef PixelSnapGM INHERITED; | 174 typedef PixelSnapGM INHERITED; |
| 185 }; | 175 }; |
| 186 | 176 |
| 187 ////////////////////////////////////////////////////////////////////////////// | 177 ////////////////////////////////////////////////////////////////////////////// |
| 188 DEF_GM( return SkNEW(PointSnapGM); ) | 178 DEF_GM( return SkNEW(PointSnapGM); ) |
| 189 DEF_GM( return SkNEW(LineSnapGM); ) | 179 DEF_GM( return SkNEW(LineSnapGM); ) |
| 190 DEF_GM( return SkNEW(RectSnapGM); ) | 180 DEF_GM( return SkNEW(RectSnapGM); ) |
| 191 DEF_GM( return SkNEW(ComboSnapGM); ) | 181 DEF_GM( return SkNEW(ComboSnapGM); ) |
| OLD | NEW |