EmptyPathTest.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright 2011 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 "include/core/SkBitmap.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkPath.h"
  10. #include "tests/Test.h"
  11. #define DIMENSION 32
  12. static void drawAndTest(skiatest::Reporter* reporter, const SkPath& path,
  13. const SkPaint& paint, bool shouldDraw) {
  14. SkBitmap bm;
  15. bm.allocN32Pixels(DIMENSION, DIMENSION);
  16. SkASSERT(DIMENSION*4 == bm.rowBytes()); // ensure no padding on each row
  17. bm.eraseColor(SK_ColorTRANSPARENT);
  18. SkCanvas canvas(bm);
  19. SkPaint p(paint);
  20. p.setColor(SK_ColorWHITE);
  21. canvas.drawPath(path, p);
  22. size_t count = DIMENSION * DIMENSION;
  23. const SkPMColor* ptr = bm.getAddr32(0, 0);
  24. SkPMColor andValue = ~0U;
  25. SkPMColor orValue = 0;
  26. for (size_t i = 0; i < count; ++i) {
  27. SkPMColor c = ptr[i];
  28. andValue &= c;
  29. orValue |= c;
  30. }
  31. // success means we drew everywhere or nowhere (depending on shouldDraw)
  32. bool success = shouldDraw ? (~0U == andValue) : (0 == orValue);
  33. if (!success) {
  34. const char* str;
  35. if (shouldDraw) {
  36. str = "Path expected to draw everywhere, but didn't. ";
  37. } else {
  38. str = "Path expected to draw nowhere, but did. ";
  39. }
  40. ERRORF(reporter, "%s style[%d] cap[%d] join[%d] antialias[%d]"
  41. " filltype[%d] ptcount[%d]", str, paint.getStyle(),
  42. paint.getStrokeCap(), paint.getStrokeJoin(),
  43. paint.isAntiAlias(), path.getFillType(), path.countPoints());
  44. // uncomment this if you want to step in to see the failure
  45. // canvas.drawPath(path, p);
  46. }
  47. }
  48. enum DrawCaps {
  49. kDontDrawCaps,
  50. kDrawCaps
  51. };
  52. static void iter_paint(skiatest::Reporter* reporter, const SkPath& path, bool shouldDraw,
  53. DrawCaps drawCaps) {
  54. static const SkPaint::Cap gCaps[] = {
  55. SkPaint::kButt_Cap,
  56. SkPaint::kRound_Cap,
  57. SkPaint::kSquare_Cap
  58. };
  59. static const SkPaint::Join gJoins[] = {
  60. SkPaint::kMiter_Join,
  61. SkPaint::kRound_Join,
  62. SkPaint::kBevel_Join
  63. };
  64. static const SkPaint::Style gStyles[] = {
  65. SkPaint::kFill_Style,
  66. SkPaint::kStroke_Style,
  67. SkPaint::kStrokeAndFill_Style
  68. };
  69. for (size_t cap = 0; cap < SK_ARRAY_COUNT(gCaps); ++cap) {
  70. for (size_t join = 0; join < SK_ARRAY_COUNT(gJoins); ++join) {
  71. for (size_t style = 0; style < SK_ARRAY_COUNT(gStyles); ++style) {
  72. if (drawCaps && SkPaint::kButt_Cap != gCaps[cap]
  73. && SkPaint::kFill_Style != gStyles[style]) {
  74. continue;
  75. }
  76. SkPaint paint;
  77. paint.setStrokeWidth(SkIntToScalar(10));
  78. paint.setStrokeCap(gCaps[cap]);
  79. paint.setStrokeJoin(gJoins[join]);
  80. paint.setStyle(gStyles[style]);
  81. paint.setAntiAlias(false);
  82. drawAndTest(reporter, path, paint, shouldDraw);
  83. paint.setAntiAlias(true);
  84. drawAndTest(reporter, path, paint, shouldDraw);
  85. }
  86. }
  87. }
  88. }
  89. #define CX (SkIntToScalar(DIMENSION) / 2)
  90. #define CY (SkIntToScalar(DIMENSION) / 2)
  91. static void make_empty(SkPath*) {}
  92. static void make_M(SkPath* path) { path->moveTo(CX, CY); }
  93. static void make_MM(SkPath* path) { path->moveTo(CX, CY).moveTo(CX, CY); }
  94. static void make_MZM(SkPath* path) { path->moveTo(CX, CY).close().moveTo(CX, CY); }
  95. static void make_L(SkPath* path) { path->moveTo(CX, CY).lineTo(CX, CY); }
  96. static void make_Q(SkPath* path) { path->moveTo(CX, CY).quadTo(CX, CY, CX, CY); }
  97. static void make_C(SkPath* path) { path->moveTo(CX, CY).cubicTo(CX, CY, CX, CY, CX, CY); }
  98. /* Two invariants are tested: How does an empty/degenerate path draw?
  99. * - if the path is drawn inverse, it should draw everywhere
  100. * - if the path is drawn non-inverse, it should draw nowhere
  101. *
  102. * Things to iterate on:
  103. * - path (empty, degenerate line/quad/cubic w/ and w/o close
  104. * - paint style
  105. * - path filltype
  106. * - path stroke variants (e.g. caps, joins, width)
  107. */
  108. static void test_emptydrawing(skiatest::Reporter* reporter) {
  109. static void (*gMakeProc[])(SkPath*) = {
  110. make_empty, make_M, make_MM, make_MZM, make_L, make_Q, make_C
  111. };
  112. static SkPath::FillType gFills[] = {
  113. SkPath::kWinding_FillType,
  114. SkPath::kEvenOdd_FillType,
  115. SkPath::kInverseWinding_FillType,
  116. SkPath::kInverseEvenOdd_FillType
  117. };
  118. for (int doClose = 0; doClose < 2; ++doClose) {
  119. for (size_t i = 0; i < SK_ARRAY_COUNT(gMakeProc); ++i) {
  120. SkPath path;
  121. gMakeProc[i](&path);
  122. if (doClose) {
  123. path.close();
  124. }
  125. /* zero length segments and close following moves draw round and square caps */
  126. bool allowCaps = make_L == gMakeProc[i] || make_Q == gMakeProc[i]
  127. || make_C == gMakeProc[i] || make_MZM == gMakeProc[i];
  128. allowCaps |= SkToBool(doClose);
  129. for (size_t fill = 0; fill < SK_ARRAY_COUNT(gFills); ++fill) {
  130. path.setFillType(gFills[fill]);
  131. bool shouldDraw = path.isInverseFillType();
  132. iter_paint(reporter, path, shouldDraw, allowCaps ? kDrawCaps : kDontDrawCaps);
  133. }
  134. }
  135. }
  136. }
  137. DEF_TEST(EmptyPath, reporter) {
  138. test_emptydrawing(reporter);
  139. }