complexclip.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 "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/SkFontTypes.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkPath.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/SkTypeface.h"
  20. #include "include/core/SkTypes.h"
  21. #include "src/core/SkClipOpPriv.h"
  22. #include "tools/ToolUtils.h"
  23. #include <string.h>
  24. namespace skiagm {
  25. constexpr SkColor gPathColor = SK_ColorBLACK;
  26. constexpr SkColor gClipAColor = SK_ColorBLUE;
  27. constexpr SkColor gClipBColor = SK_ColorRED;
  28. class ComplexClipGM : public GM {
  29. public:
  30. ComplexClipGM(bool aaclip, bool saveLayer, bool invertDraw)
  31. : fDoAAClip(aaclip)
  32. , fDoSaveLayer(saveLayer)
  33. , fInvertDraw(invertDraw) {
  34. this->setBGColor(0xFFDEDFDE);
  35. }
  36. protected:
  37. SkString onShortName() override {
  38. SkString str;
  39. str.printf("complexclip_%s%s%s",
  40. fDoAAClip ? "aa" : "bw",
  41. fDoSaveLayer ? "_layer" : "",
  42. fInvertDraw ? "_invert" : "");
  43. return str;
  44. }
  45. SkISize onISize() override { return SkISize::Make(970, 780); }
  46. void onDraw(SkCanvas* canvas) override {
  47. SkPath path;
  48. path.moveTo(0, 50)
  49. .quadTo(0, 0, 50, 0)
  50. .lineTo(175, 0)
  51. .quadTo(200, 0, 200, 25)
  52. .lineTo(200, 150)
  53. .quadTo(200, 200, 150, 200)
  54. .lineTo(0, 200)
  55. .close()
  56. .moveTo(50, 50)
  57. .lineTo(150, 50)
  58. .lineTo(150, 125)
  59. .quadTo(150, 150, 125, 150)
  60. .lineTo(50, 150)
  61. .close();
  62. if (fInvertDraw) {
  63. path.setFillType(SkPath::kInverseEvenOdd_FillType);
  64. } else {
  65. path.setFillType(SkPath::kEvenOdd_FillType);
  66. }
  67. SkPaint pathPaint;
  68. pathPaint.setAntiAlias(true);
  69. pathPaint.setColor(gPathColor);
  70. SkPath clipA;
  71. clipA.addPoly({{10, 20}, {165, 22}, {70, 105}, {165, 177}, {-5, 180}}, false).close();
  72. SkPath clipB;
  73. clipB.addPoly({{40, 10}, {190, 15}, {195, 190}, {40, 185}, {155, 100}}, false).close();
  74. SkFont font(ToolUtils::create_portable_typeface(), 20);
  75. constexpr struct {
  76. SkClipOp fOp;
  77. const char* fName;
  78. } gOps[] = { //extra spaces in names for measureText
  79. {kIntersect_SkClipOp, "Isect "},
  80. {kDifference_SkClipOp, "Diff " },
  81. {kUnion_SkClipOp, "Union "},
  82. {kXOR_SkClipOp, "Xor " },
  83. {kReverseDifference_SkClipOp, "RDiff "}
  84. };
  85. canvas->translate(20, 20);
  86. canvas->scale(3 * SK_Scalar1 / 4, 3 * SK_Scalar1 / 4);
  87. if (fDoSaveLayer) {
  88. // We want the layer to appear symmetric relative to actual
  89. // device boundaries so we need to "undo" the effect of the
  90. // scale and translate
  91. SkRect bounds = SkRect::MakeLTRB(
  92. 4.0f/3.0f * -20,
  93. 4.0f/3.0f * -20,
  94. 4.0f/3.0f * (this->getISize().fWidth - 20),
  95. 4.0f/3.0f * (this->getISize().fHeight - 20));
  96. bounds.inset(100, 100);
  97. SkPaint boundPaint;
  98. boundPaint.setColor(SK_ColorRED);
  99. boundPaint.setStyle(SkPaint::kStroke_Style);
  100. canvas->drawRect(bounds, boundPaint);
  101. canvas->clipRect(bounds);
  102. canvas->saveLayer(&bounds, nullptr);
  103. }
  104. for (int invBits = 0; invBits < 4; ++invBits) {
  105. canvas->save();
  106. for (size_t op = 0; op < SK_ARRAY_COUNT(gOps); ++op) {
  107. this->drawHairlines(canvas, path, clipA, clipB);
  108. bool doInvA = SkToBool(invBits & 1);
  109. bool doInvB = SkToBool(invBits & 2);
  110. canvas->save();
  111. // set clip
  112. clipA.setFillType(doInvA ? SkPath::kInverseEvenOdd_FillType :
  113. SkPath::kEvenOdd_FillType);
  114. clipB.setFillType(doInvB ? SkPath::kInverseEvenOdd_FillType :
  115. SkPath::kEvenOdd_FillType);
  116. canvas->clipPath(clipA, fDoAAClip);
  117. canvas->clipPath(clipB, gOps[op].fOp, fDoAAClip);
  118. // In the inverse case we need to prevent the draw from covering the whole
  119. // canvas.
  120. if (fInvertDraw) {
  121. SkRect rectClip = clipA.getBounds();
  122. rectClip.join(path.getBounds());
  123. rectClip.join(path.getBounds());
  124. rectClip.outset(5, 5);
  125. canvas->clipRect(rectClip);
  126. }
  127. // draw path clipped
  128. canvas->drawPath(path, pathPaint);
  129. canvas->restore();
  130. SkPaint paint;
  131. SkScalar txtX = 45;
  132. paint.setColor(gClipAColor);
  133. const char* aTxt = doInvA ? "InvA " : "A ";
  134. canvas->drawSimpleText(aTxt, strlen(aTxt), SkTextEncoding::kUTF8, txtX, 220, font, paint);
  135. txtX += font.measureText(aTxt, strlen(aTxt), SkTextEncoding::kUTF8);
  136. paint.setColor(SK_ColorBLACK);
  137. canvas->drawSimpleText(gOps[op].fName, strlen(gOps[op].fName), SkTextEncoding::kUTF8, txtX, 220,
  138. font, paint);
  139. txtX += font.measureText(gOps[op].fName, strlen(gOps[op].fName), SkTextEncoding::kUTF8);
  140. paint.setColor(gClipBColor);
  141. const char* bTxt = doInvB ? "InvB " : "B ";
  142. canvas->drawSimpleText(bTxt, strlen(bTxt), SkTextEncoding::kUTF8, txtX, 220, font, paint);
  143. canvas->translate(250,0);
  144. }
  145. canvas->restore();
  146. canvas->translate(0, 250);
  147. }
  148. if (fDoSaveLayer) {
  149. canvas->restore();
  150. }
  151. }
  152. private:
  153. void drawHairlines(SkCanvas* canvas, const SkPath& path,
  154. const SkPath& clipA, const SkPath& clipB) {
  155. SkPaint paint;
  156. paint.setAntiAlias(true);
  157. paint.setStyle(SkPaint::kStroke_Style);
  158. const SkAlpha fade = 0x33;
  159. // draw path in hairline
  160. paint.setColor(gPathColor); paint.setAlpha(fade);
  161. canvas->drawPath(path, paint);
  162. // draw clips in hair line
  163. paint.setColor(gClipAColor); paint.setAlpha(fade);
  164. canvas->drawPath(clipA, paint);
  165. paint.setColor(gClipBColor); paint.setAlpha(fade);
  166. canvas->drawPath(clipB, paint);
  167. }
  168. bool fDoAAClip;
  169. bool fDoSaveLayer;
  170. bool fInvertDraw;
  171. typedef GM INHERITED;
  172. };
  173. //////////////////////////////////////////////////////////////////////////////
  174. DEF_GM(return new ComplexClipGM(false, false, false);)
  175. DEF_GM(return new ComplexClipGM(false, false, true);)
  176. DEF_GM(return new ComplexClipGM(false, true, false);)
  177. DEF_GM(return new ComplexClipGM(false, true, true);)
  178. DEF_GM(return new ComplexClipGM(true, false, false);)
  179. DEF_GM(return new ComplexClipGM(true, false, true);)
  180. DEF_GM(return new ComplexClipGM(true, true, false);)
  181. DEF_GM(return new ComplexClipGM(true, true, true);)
  182. }