complexclip3.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright 2014 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/SkClipOp.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkFont.h"
  12. #include "include/core/SkPaint.h"
  13. #include "include/core/SkPath.h"
  14. #include "include/core/SkRect.h"
  15. #include "include/core/SkScalar.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 "src/core/SkClipOpPriv.h"
  21. #include "tools/ToolUtils.h"
  22. #include <utility>
  23. namespace skiagm {
  24. constexpr SkColor gPathColor = SK_ColorYELLOW;
  25. class ComplexClip3GM : public GM {
  26. public:
  27. ComplexClip3GM(bool doSimpleClipFirst)
  28. : fDoSimpleClipFirst(doSimpleClipFirst) {
  29. this->setBGColor(0xFFDDDDDD);
  30. }
  31. protected:
  32. SkString onShortName() {
  33. SkString str;
  34. str.printf("complexclip3_%s", fDoSimpleClipFirst ? "simple" : "complex");
  35. return str;
  36. }
  37. SkISize onISize() { return SkISize::Make(1000, 950); }
  38. virtual void onDraw(SkCanvas* canvas) {
  39. SkPath clipSimple;
  40. clipSimple.addCircle(SkIntToScalar(70), SkIntToScalar(50), SkIntToScalar(20));
  41. SkRect r1 = { 10, 20, 70, 80 };
  42. SkPath clipComplex;
  43. clipComplex.moveTo(SkIntToScalar(40), SkIntToScalar(50));
  44. clipComplex.arcTo(r1, SkIntToScalar(30), SkIntToScalar(300), false);
  45. clipComplex.close();
  46. SkPath* firstClip = &clipSimple;
  47. SkPath* secondClip = &clipComplex;
  48. if (!fDoSimpleClipFirst) {
  49. using std::swap;
  50. swap(firstClip, secondClip);
  51. }
  52. SkPaint paint;
  53. paint.setAntiAlias(true);
  54. SkFont font(ToolUtils::create_portable_typeface(), 20);
  55. constexpr struct {
  56. SkClipOp fOp;
  57. const char* fName;
  58. } gOps[] = {
  59. {kIntersect_SkClipOp, "I"},
  60. {kDifference_SkClipOp, "D" },
  61. {kUnion_SkClipOp, "U"},
  62. {kXOR_SkClipOp, "X" },
  63. {kReverseDifference_SkClipOp, "R"}
  64. };
  65. canvas->translate(SkIntToScalar(20), SkIntToScalar(20));
  66. canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4);
  67. SkPaint pathPaint;
  68. pathPaint.setAntiAlias(true);
  69. pathPaint.setColor(gPathColor);
  70. for (int invA = 0; invA < 2; ++invA) {
  71. for (int aaBits = 0; aaBits < 4; ++aaBits) {
  72. canvas->save();
  73. for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
  74. for (int invB = 0; invB < 2; ++invB) {
  75. bool doAAA = SkToBool(aaBits & 1);
  76. bool doAAB = SkToBool(aaBits & 2);
  77. bool doInvA = SkToBool(invA);
  78. bool doInvB = SkToBool(invB);
  79. canvas->save();
  80. // set clip
  81. firstClip->setFillType(doInvA ? SkPath::kInverseEvenOdd_FillType :
  82. SkPath::kEvenOdd_FillType);
  83. secondClip->setFillType(doInvB ? SkPath::kInverseEvenOdd_FillType :
  84. SkPath::kEvenOdd_FillType);
  85. canvas->clipPath(*firstClip, doAAA);
  86. canvas->clipPath(*secondClip, gOps[op].fOp, doAAB);
  87. // draw rect clipped
  88. SkRect r = { 0, 0, 100, 100 };
  89. canvas->drawRect(r, pathPaint);
  90. canvas->restore();
  91. SkScalar txtX = SkIntToScalar(10);
  92. paint.setColor(SK_ColorBLACK);
  93. SkString str;
  94. str.printf("%s%s %s %s%s", doAAA ? "A" : "B",
  95. doInvA ? "I" : "N",
  96. gOps[op].fName,
  97. doAAB ? "A" : "B",
  98. doInvB ? "I" : "N");
  99. canvas->drawString(str.c_str(), txtX, SkIntToScalar(130), font, paint);
  100. if (doInvB) {
  101. canvas->translate(SkIntToScalar(150),0);
  102. } else {
  103. canvas->translate(SkIntToScalar(120),0);
  104. }
  105. }
  106. }
  107. canvas->restore();
  108. canvas->translate(0, SkIntToScalar(150));
  109. }
  110. }
  111. }
  112. private:
  113. bool fDoSimpleClipFirst;
  114. typedef GM INHERITED;
  115. };
  116. //////////////////////////////////////////////////////////////////////////////
  117. // Simple clip first
  118. DEF_GM( return new ComplexClip3GM(true); )
  119. // Complex clip first
  120. DEF_GM( return new ComplexClip3GM(false); )
  121. }