pixelsnap.cpp 6.8 KB

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