complexclip2.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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/SkPaint.h"
  12. #include "include/core/SkPath.h"
  13. #include "include/core/SkRRect.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/SkTypes.h"
  19. #include "include/utils/SkRandom.h"
  20. #include "src/core/SkClipOpPriv.h"
  21. namespace skiagm {
  22. class ComplexClip2GM : public GM {
  23. public:
  24. enum Clip {
  25. kRect_Clip,
  26. kRRect_Clip,
  27. kPath_Clip
  28. };
  29. ComplexClip2GM(Clip clip, bool antiAlias)
  30. : fClip(clip)
  31. , fAntiAlias(antiAlias) {
  32. SkScalar xA = 0.65f;
  33. SkScalar xF = 50.65f;
  34. SkScalar yA = 0.65f;
  35. SkScalar yF = 50.65f;
  36. fWidth = xF - xA;
  37. fHeight = yF - yA;
  38. fTotalWidth = kCols * fWidth + SK_Scalar1 * (kCols + 1) * kPadX;
  39. fTotalHeight = kRows * fHeight + SK_Scalar1 * (kRows + 1) * kPadY;
  40. }
  41. protected:
  42. void onOnceBeforeDraw() override {
  43. this->setBGColor(SkColorSetRGB(0xDD,0xA0,0xDD));
  44. // offset the rects a bit so we get antialiasing even in the rect case
  45. SkScalar xA = 0.65f;
  46. SkScalar xB = 10.65f;
  47. SkScalar xC = 20.65f;
  48. SkScalar xD = 30.65f;
  49. SkScalar xE = 40.65f;
  50. SkScalar xF = 50.65f;
  51. SkScalar yA = 0.65f;
  52. SkScalar yB = 10.65f;
  53. SkScalar yC = 20.65f;
  54. SkScalar yD = 30.65f;
  55. SkScalar yE = 40.65f;
  56. SkScalar yF = 50.65f;
  57. fRects[0].set(xB, yB, xE, yE);
  58. fRRects[0].setRectXY(fRects[0], 7, 7);
  59. fPaths[0].addRoundRect(fRects[0], 5, 5);
  60. fRectColors[0] = SK_ColorRED;
  61. fRects[1].set(xA, yA, xD, yD);
  62. fRRects[1].setRectXY(fRects[1], 7, 7);
  63. fPaths[1].addRoundRect(fRects[1], 5, 5);
  64. fRectColors[1] = SK_ColorGREEN;
  65. fRects[2].set(xC, yA, xF, yD);
  66. fRRects[2].setRectXY(fRects[2], 7, 7);
  67. fPaths[2].addRoundRect(fRects[2], 5, 5);
  68. fRectColors[2] = SK_ColorBLUE;
  69. fRects[3].set(xA, yC, xD, yF);
  70. fRRects[3].setRectXY(fRects[3], 7, 7);
  71. fPaths[3].addRoundRect(fRects[3], 5, 5);
  72. fRectColors[3] = SK_ColorYELLOW;
  73. fRects[4].set(xC, yC, xF, yF);
  74. fRRects[4].setRectXY(fRects[4], 7, 7);
  75. fPaths[4].addRoundRect(fRects[4], 5, 5);
  76. fRectColors[4] = SK_ColorCYAN;
  77. const SkClipOp ops[] = {
  78. kDifference_SkClipOp,
  79. kIntersect_SkClipOp,
  80. kUnion_SkClipOp,
  81. kXOR_SkClipOp,
  82. kReverseDifference_SkClipOp,
  83. kReplace_SkClipOp,
  84. };
  85. SkRandom r;
  86. for (int i = 0; i < kRows; ++i) {
  87. for (int j = 0; j < kCols; ++j) {
  88. for (int k = 0; k < 5; ++k) {
  89. fOps[j*kRows+i][k] = ops[r.nextU() % SK_ARRAY_COUNT(ops)];
  90. }
  91. }
  92. }
  93. }
  94. static constexpr int kRows = 5;
  95. static constexpr int kCols = 5;
  96. static constexpr int kPadX = 20;
  97. static constexpr int kPadY = 20;
  98. static const char* ClipStr(Clip clip) {
  99. switch (clip) {
  100. case kRect_Clip:
  101. return "rect";
  102. case kRRect_Clip:
  103. return "rrect";
  104. case kPath_Clip:
  105. return "path";
  106. }
  107. SkDEBUGFAIL("Unknown clip type.");
  108. return "";
  109. }
  110. SkString onShortName() override {
  111. if (kRect_Clip == fClip && !fAntiAlias) {
  112. return SkString("complexclip2");
  113. }
  114. SkString str;
  115. str.printf("complexclip2_%s_%s",
  116. ClipStr(fClip),
  117. fAntiAlias ? "aa" : "bw");
  118. return str;
  119. }
  120. SkISize onISize() override {
  121. return SkISize::Make(SkScalarRoundToInt(fTotalWidth),
  122. SkScalarRoundToInt(fTotalHeight));
  123. }
  124. void onDraw(SkCanvas* canvas) override {
  125. SkPaint rectPaint;
  126. rectPaint.setStyle(SkPaint::kStroke_Style);
  127. rectPaint.setStrokeWidth(-1);
  128. SkPaint fillPaint;
  129. fillPaint.setColor(SkColorSetRGB(0xA0,0xDD,0xA0));
  130. for (int i = 0; i < kRows; ++i) {
  131. for (int j = 0; j < kCols; ++j) {
  132. canvas->save();
  133. canvas->translate(kPadX * SK_Scalar1 + (fWidth + kPadX * SK_Scalar1)*j,
  134. kPadY * SK_Scalar1 + (fHeight + kPadY * SK_Scalar1)*i);
  135. // draw the original shapes first so we can see the
  136. // antialiasing on the clipped draw
  137. for (int k = 0; k < 5; ++k) {
  138. rectPaint.setColor(fRectColors[k]);
  139. switch (fClip) {
  140. case kRect_Clip:
  141. canvas->drawRect(fRects[k], rectPaint);
  142. break;
  143. case kRRect_Clip:
  144. canvas->drawRRect(fRRects[k], rectPaint);
  145. break;
  146. case kPath_Clip:
  147. canvas->drawPath(fPaths[k], rectPaint);
  148. break;
  149. }
  150. }
  151. for (int k = 0; k < 5; ++k) {
  152. switch (fClip) {
  153. case kRect_Clip:
  154. canvas->clipRect(fRects[k],
  155. fOps[j*kRows+i][k],
  156. fAntiAlias);
  157. break;
  158. case kRRect_Clip:
  159. canvas->clipRRect(fRRects[k],
  160. fOps[j*kRows+i][k],
  161. fAntiAlias);
  162. break;
  163. case kPath_Clip:
  164. canvas->clipPath(fPaths[k],
  165. fOps[j*kRows+i][k],
  166. fAntiAlias);
  167. break;
  168. }
  169. }
  170. canvas->drawRect(SkRect::MakeWH(fWidth, fHeight), fillPaint);
  171. canvas->restore();
  172. }
  173. }
  174. }
  175. private:
  176. Clip fClip;
  177. bool fAntiAlias;
  178. SkRect fRects[5];
  179. SkRRect fRRects[5];
  180. SkPath fPaths[5];
  181. SkColor fRectColors[5];
  182. SkClipOp fOps[kRows * kCols][5];
  183. SkScalar fWidth;
  184. SkScalar fHeight;
  185. SkScalar fTotalWidth;
  186. SkScalar fTotalHeight;
  187. typedef GM INHERITED;
  188. };
  189. //////////////////////////////////////////////////////////////////////////////
  190. // bw
  191. DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRect_Clip, false); )
  192. DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRRect_Clip, false); )
  193. DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kPath_Clip, false); )
  194. // aa
  195. DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRect_Clip, true); )
  196. DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kRRect_Clip, true); )
  197. DEF_GM( return new ComplexClip2GM(ComplexClip2GM::kPath_Clip, true); )
  198. }