polygons.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright 2013 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/SkPaint.h"
  11. #include "include/core/SkPath.h"
  12. #include "include/core/SkPoint.h"
  13. #include "include/core/SkScalar.h"
  14. #include "include/core/SkSize.h"
  15. #include "include/core/SkString.h"
  16. #include "include/core/SkTypes.h"
  17. #include "include/private/SkTArray.h"
  18. #include "include/utils/SkRandom.h"
  19. namespace skiagm {
  20. // This GM tests a grab-bag of convex and concave polygons. They are triangles,
  21. // trapezoid, diamond, polygons with lots of edges, several concave polygons...
  22. // But rectangles are excluded.
  23. class PolygonsGM: public GM {
  24. public:
  25. PolygonsGM() {}
  26. protected:
  27. SkString onShortName() override {
  28. return SkString("polygons");
  29. }
  30. SkISize onISize() override {
  31. int width = kNumPolygons * kCellSize + 40;
  32. int height = (kNumJoins * kNumStrokeWidths + kNumExtraStyles) * kCellSize + 40;
  33. return SkISize::Make(width, height);
  34. }
  35. // Construct all polygons
  36. void onOnceBeforeDraw() override {
  37. SkPoint p0[] = {{0, 0}, {60, 0}, {90, 40}}; // triangle
  38. SkPoint p1[] = {{0, 0}, {0, 40}, {60, 40}, {40, 0}}; // trapezoid
  39. SkPoint p2[] = {{0, 0}, {40, 40}, {80, 40}, {40, 0}}; // diamond
  40. SkPoint p3[] = {{10, 0}, {50, 0}, {60, 10}, {60, 30}, {50, 40},
  41. {10, 40}, {0, 30}, {0, 10}}; // octagon
  42. SkPoint p4[32]; // circle-like polygons with 32-edges.
  43. SkPoint p5[] = {{0, 0}, {20, 20}, {0, 40}, {60, 20}}; // concave polygon with 4 edges
  44. SkPoint p6[] = {{0, 40}, {0, 30}, {15, 30}, {15, 20}, {30, 20},
  45. {30, 10}, {45, 10}, {45, 0}, {60, 0}, {60, 40}}; // stairs-like polygon
  46. SkPoint p7[] = {{0, 20}, {20, 20}, {30, 0}, {40, 20}, {60, 20},
  47. {45, 30}, {55, 50}, {30, 40}, {5, 50}, {15, 30}}; // five-point stars
  48. for (size_t i = 0; i < SK_ARRAY_COUNT(p4); ++i) {
  49. SkScalar angle = 2 * SK_ScalarPI * i / SK_ARRAY_COUNT(p4);
  50. p4[i].set(20 * SkScalarCos(angle) + 20, 20 * SkScalarSin(angle) + 20);
  51. }
  52. struct Polygons {
  53. SkPoint* fPoints;
  54. size_t fPointNum;
  55. } pgs[] = {
  56. { p0, SK_ARRAY_COUNT(p0) },
  57. { p1, SK_ARRAY_COUNT(p1) },
  58. { p2, SK_ARRAY_COUNT(p2) },
  59. { p3, SK_ARRAY_COUNT(p3) },
  60. { p4, SK_ARRAY_COUNT(p4) },
  61. { p5, SK_ARRAY_COUNT(p5) },
  62. { p6, SK_ARRAY_COUNT(p6) },
  63. { p7, SK_ARRAY_COUNT(p7) }
  64. };
  65. SkASSERT(SK_ARRAY_COUNT(pgs) == kNumPolygons);
  66. for (size_t pgIndex = 0; pgIndex < SK_ARRAY_COUNT(pgs); ++pgIndex) {
  67. fPolygons.push_back().moveTo(pgs[pgIndex].fPoints[0].fX,
  68. pgs[pgIndex].fPoints[0].fY);
  69. for (size_t ptIndex = 1; ptIndex < pgs[pgIndex].fPointNum; ++ptIndex) {
  70. fPolygons.back().lineTo(pgs[pgIndex].fPoints[ptIndex].fX,
  71. pgs[pgIndex].fPoints[ptIndex].fY);
  72. }
  73. fPolygons.back().close();
  74. }
  75. }
  76. // Set the location for the current test on the canvas
  77. static void SetLocation(SkCanvas* canvas, int counter, int lineNum) {
  78. SkScalar x = SK_Scalar1 * kCellSize * (counter % lineNum) + 30 + SK_Scalar1 / 4;
  79. SkScalar y = SK_Scalar1 * kCellSize * (counter / lineNum) + 30 + 3 * SK_Scalar1 / 4;
  80. canvas->translate(x, y);
  81. }
  82. static void SetColorAndAlpha(SkPaint* paint, SkRandom* rand) {
  83. SkColor color = rand->nextU();
  84. color |= 0xff000000;
  85. paint->setColor(color);
  86. if (40 == paint->getStrokeWidth()) {
  87. paint->setAlpha(0xA0);
  88. }
  89. }
  90. void onDraw(SkCanvas* canvas) override {
  91. // Stroke widths are:
  92. // 0(may use hairline rendering), 10(common case for stroke-style)
  93. // 40(>= geometry width/height, make the contour filled in fact)
  94. constexpr int kStrokeWidths[] = {0, 10, 40};
  95. SkASSERT(kNumStrokeWidths == SK_ARRAY_COUNT(kStrokeWidths));
  96. constexpr SkPaint::Join kJoins[] = {
  97. SkPaint::kMiter_Join, SkPaint::kRound_Join, SkPaint::kBevel_Join
  98. };
  99. SkASSERT(kNumJoins == SK_ARRAY_COUNT(kJoins));
  100. int counter = 0;
  101. SkPaint paint;
  102. paint.setAntiAlias(true);
  103. SkRandom rand;
  104. // For stroke style painter
  105. paint.setStyle(SkPaint::kStroke_Style);
  106. for (int join = 0; join < kNumJoins; ++join) {
  107. for (int width = 0; width < kNumStrokeWidths; ++width) {
  108. for (int i = 0; i < fPolygons.count(); ++i) {
  109. canvas->save();
  110. SetLocation(canvas, counter, fPolygons.count());
  111. SetColorAndAlpha(&paint, &rand);
  112. paint.setStrokeJoin(kJoins[join]);
  113. paint.setStrokeWidth(SkIntToScalar(kStrokeWidths[width]));
  114. canvas->drawPath(fPolygons[i], paint);
  115. canvas->restore();
  116. ++counter;
  117. }
  118. }
  119. }
  120. // For stroke-and-fill style painter and fill style painter
  121. constexpr SkPaint::Style kStyles[] = {
  122. SkPaint::kStrokeAndFill_Style, SkPaint::kFill_Style
  123. };
  124. SkASSERT(kNumExtraStyles == SK_ARRAY_COUNT(kStyles));
  125. paint.setStrokeJoin(SkPaint::kMiter_Join);
  126. paint.setStrokeWidth(SkIntToScalar(20));
  127. for (int style = 0; style < kNumExtraStyles; ++style) {
  128. paint.setStyle(kStyles[style]);
  129. for (int i = 0; i < fPolygons.count(); ++i) {
  130. canvas->save();
  131. SetLocation(canvas, counter, fPolygons.count());
  132. SetColorAndAlpha(&paint, &rand);
  133. canvas->drawPath(fPolygons[i], paint);
  134. canvas->restore();
  135. ++counter;
  136. }
  137. }
  138. }
  139. private:
  140. static constexpr int kNumPolygons = 8;
  141. static constexpr int kCellSize = 100;
  142. static constexpr int kNumExtraStyles = 2;
  143. static constexpr int kNumStrokeWidths = 3;
  144. static constexpr int kNumJoins = 3;
  145. SkTArray<SkPath> fPolygons;
  146. typedef GM INHERITED;
  147. };
  148. //////////////////////////////////////////////////////////////////////////////
  149. DEF_GM(return new PolygonsGM;)
  150. }