shapes.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright 2016 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/SkImageInfo.h"
  11. #include "include/core/SkMatrix.h"
  12. #include "include/core/SkPaint.h"
  13. #include "include/core/SkPoint.h"
  14. #include "include/core/SkRRect.h"
  15. #include "include/core/SkRect.h"
  16. #include "include/core/SkScalar.h"
  17. #include "include/core/SkSize.h"
  18. #include "include/core/SkString.h"
  19. #include "include/core/SkTypes.h"
  20. #include "include/private/SkTArray.h"
  21. #include "include/utils/SkRandom.h"
  22. namespace skiagm {
  23. /*
  24. * This is the base class for two GMs that cover various corner cases with primitive Skia shapes
  25. * (zero radius, near-zero radius, inner shape overlap, etc.) It uses an xfermode of darken to help
  26. * double-blended and/or dropped pixels stand out.
  27. */
  28. class ShapesGM : public GM {
  29. protected:
  30. ShapesGM(const char* name, bool antialias) : fName(name), fAntialias(antialias) {
  31. if (!antialias) {
  32. fName.append("_bw");
  33. }
  34. }
  35. SkString onShortName() override final { return fName; }
  36. SkISize onISize() override { return SkISize::Make(500, 500); }
  37. void onOnceBeforeDraw() override {
  38. fShapes.push_back().setOval(SkRect::MakeXYWH(-5, 25, 200, 100));
  39. fRotations.push_back(21);
  40. fShapes.push_back().setRect(SkRect::MakeXYWH(95, 75, 125, 100));
  41. fRotations.push_back(94);
  42. fShapes.push_back().setRectXY(SkRect::MakeXYWH(0, 75, 150, 100), 1e-5f, 1e-5f);
  43. fRotations.push_back(132);
  44. fShapes.push_back().setRectXY(SkRect::MakeXYWH(15, -20, 100, 100), 20, 15);
  45. fRotations.push_back(282);
  46. fSimpleShapeCount = fShapes.count();
  47. fShapes.push_back().setNinePatch(SkRect::MakeXYWH(140, -50, 90, 110), 10, 5, 25, 35);
  48. fRotations.push_back(0);
  49. fShapes.push_back().setNinePatch(SkRect::MakeXYWH(160, -60, 60, 90), 10, 60, 50, 30);
  50. fRotations.push_back(-35);
  51. fShapes.push_back().setNinePatch(SkRect::MakeXYWH(220, -120, 60, 90), 1, 89, 59, 1);
  52. fRotations.push_back(65);
  53. SkVector radii[4] = {{4, 6}, {12, 8}, {24, 16}, {32, 48}};
  54. fShapes.push_back().setRectRadii(SkRect::MakeXYWH(150, -129, 80, 160), radii);
  55. fRotations.push_back(265);
  56. SkVector radii2[4] = {{0, 0}, {80, 60}, {0, 0}, {80, 60}};
  57. fShapes.push_back().setRectRadii(SkRect::MakeXYWH(180, -30, 80, 60), radii2);
  58. fRotations.push_back(295);
  59. fPaint.setAntiAlias(fAntialias);
  60. }
  61. void onDraw(SkCanvas* canvas) override {
  62. canvas->clear(SK_ColorWHITE);
  63. canvas->save();
  64. canvas->translate(canvas->imageInfo().width() / 2.f, canvas->imageInfo().height() / 2.f);
  65. this->drawShapes(canvas);
  66. canvas->restore();
  67. }
  68. virtual void drawShapes(SkCanvas* canvas) const = 0;
  69. protected:
  70. SkString fName;
  71. bool fAntialias;
  72. SkPaint fPaint;
  73. SkTArray<SkRRect> fShapes;
  74. SkTArray<SkScalar> fRotations;
  75. int fSimpleShapeCount;
  76. private:
  77. typedef GM INHERITED;
  78. };
  79. class SimpleShapesGM : public ShapesGM {
  80. public:
  81. SimpleShapesGM(bool antialias) : INHERITED("simpleshapes", antialias) {}
  82. private:
  83. void drawShapes(SkCanvas* canvas) const override {
  84. SkRandom rand(2);
  85. for (int i = 0; i < fShapes.count(); i++) {
  86. SkPaint paint(fPaint);
  87. paint.setColor(rand.nextU() & ~0x808080);
  88. paint.setAlphaf(0.5f); // Use alpha to detect double blends.
  89. const SkRRect& shape = fShapes[i];
  90. canvas->save();
  91. canvas->rotate(fRotations[i]);
  92. switch (shape.getType()) {
  93. case SkRRect::kRect_Type:
  94. canvas->drawRect(shape.rect(), paint);
  95. break;
  96. case SkRRect::kOval_Type:
  97. canvas->drawOval(shape.rect(), paint);
  98. break;
  99. default:
  100. canvas->drawRRect(shape, paint);
  101. break;
  102. }
  103. canvas->restore();
  104. }
  105. }
  106. typedef ShapesGM INHERITED;
  107. };
  108. class InnerShapesGM : public ShapesGM {
  109. public:
  110. InnerShapesGM(bool antialias) : INHERITED("innershapes", antialias) {}
  111. private:
  112. void drawShapes(SkCanvas* canvas) const override {
  113. SkRandom rand;
  114. for (int i = 0; i < fShapes.count(); i++) {
  115. const SkRRect& outer = fShapes[i];
  116. const SkRRect& inner = fShapes[(i * 7 + 11) % fSimpleShapeCount];
  117. float s = 0.95f * SkTMin(outer.rect().width() / inner.rect().width(),
  118. outer.rect().height() / inner.rect().height());
  119. SkMatrix innerXform;
  120. float dx = (rand.nextF() - 0.5f) * (outer.rect().width() - s * inner.rect().width());
  121. float dy = (rand.nextF() - 0.5f) * (outer.rect().height() - s * inner.rect().height());
  122. innerXform.setTranslate(outer.rect().centerX() + dx, outer.rect().centerY() + dy);
  123. if (s < 1) {
  124. innerXform.preScale(s, s);
  125. }
  126. innerXform.preTranslate(-inner.rect().centerX(), -inner.rect().centerY());
  127. SkRRect xformedInner;
  128. inner.transform(innerXform, &xformedInner);
  129. SkPaint paint(fPaint);
  130. paint.setColor(rand.nextU() & ~0x808080);
  131. paint.setAlphaf(0.5f); // Use alpha to detect double blends.
  132. canvas->save();
  133. canvas->rotate(fRotations[i]);
  134. canvas->drawDRRect(outer, xformedInner, paint);
  135. canvas->restore();
  136. }
  137. }
  138. typedef ShapesGM INHERITED;
  139. };
  140. //////////////////////////////////////////////////////////////////////////////
  141. DEF_GM( return new SimpleShapesGM(true); )
  142. DEF_GM( return new SimpleShapesGM(false); )
  143. DEF_GM( return new InnerShapesGM(true); )
  144. DEF_GM( return new InnerShapesGM(false); )
  145. }