AAClipBench.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 "bench/Benchmark.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkPath.h"
  10. #include "include/core/SkRegion.h"
  11. #include "include/core/SkString.h"
  12. #include "include/utils/SkRandom.h"
  13. #include "src/core/SkAAClip.h"
  14. #include "src/core/SkClipOpPriv.h"
  15. ////////////////////////////////////////////////////////////////////////////////
  16. // This bench tests out AA/BW clipping via canvas' clipPath and clipRect calls
  17. class AAClipBench : public Benchmark {
  18. SkString fName;
  19. SkPath fClipPath;
  20. SkRect fClipRect;
  21. SkRect fDrawRect;
  22. bool fDoPath;
  23. bool fDoAA;
  24. public:
  25. AAClipBench(bool doPath, bool doAA)
  26. : fDoPath(doPath)
  27. , fDoAA(doAA) {
  28. fName.printf("aaclip_%s_%s",
  29. doPath ? "path" : "rect",
  30. doAA ? "AA" : "BW");
  31. fClipRect.set(10.5f, 10.5f,
  32. 50.5f, 50.5f);
  33. fClipPath.addRoundRect(fClipRect, SkIntToScalar(10), SkIntToScalar(10));
  34. fDrawRect.set(SkIntToScalar(0), SkIntToScalar(0),
  35. SkIntToScalar(100), SkIntToScalar(100));
  36. SkASSERT(fClipPath.isConvex());
  37. }
  38. protected:
  39. virtual const char* onGetName() { return fName.c_str(); }
  40. virtual void onDraw(int loops, SkCanvas* canvas) {
  41. SkPaint paint;
  42. this->setupPaint(&paint);
  43. for (int i = 0; i < loops; ++i) {
  44. // jostle the clip regions each time to prevent caching
  45. fClipRect.offset((i % 2) == 0 ? SkIntToScalar(10) : SkIntToScalar(-10), 0);
  46. fClipPath.reset();
  47. fClipPath.addRoundRect(fClipRect,
  48. SkIntToScalar(5), SkIntToScalar(5));
  49. SkASSERT(fClipPath.isConvex());
  50. canvas->save();
  51. #if 1
  52. if (fDoPath) {
  53. canvas->clipPath(fClipPath, kReplace_SkClipOp, fDoAA);
  54. } else {
  55. canvas->clipRect(fClipRect, kReplace_SkClipOp, fDoAA);
  56. }
  57. canvas->drawRect(fDrawRect, paint);
  58. #else
  59. // this path tests out directly draw the clip primitive
  60. // use it to comparing just drawing the clip vs. drawing using
  61. // the clip
  62. if (fDoPath) {
  63. canvas->drawPath(fClipPath, paint);
  64. } else {
  65. canvas->drawRect(fClipRect, paint);
  66. }
  67. #endif
  68. canvas->restore();
  69. }
  70. }
  71. private:
  72. typedef Benchmark INHERITED;
  73. };
  74. ////////////////////////////////////////////////////////////////////////////////
  75. // This bench tests out nested clip stacks. It is intended to simulate
  76. // how WebKit nests clips.
  77. class NestedAAClipBench : public Benchmark {
  78. SkString fName;
  79. bool fDoAA;
  80. SkRect fDrawRect;
  81. SkRandom fRandom;
  82. static const int kNestingDepth = 3;
  83. static const int kImageSize = 400;
  84. SkPoint fSizes[kNestingDepth+1];
  85. public:
  86. NestedAAClipBench(bool doAA) : fDoAA(doAA) {
  87. fName.printf("nested_aaclip_%s", doAA ? "AA" : "BW");
  88. fDrawRect = SkRect::MakeLTRB(0, 0,
  89. SkIntToScalar(kImageSize),
  90. SkIntToScalar(kImageSize));
  91. fSizes[0].set(SkIntToScalar(kImageSize), SkIntToScalar(kImageSize));
  92. for (int i = 1; i < kNestingDepth+1; ++i) {
  93. fSizes[i].set(fSizes[i-1].fX/2, fSizes[i-1].fY/2);
  94. }
  95. }
  96. protected:
  97. virtual const char* onGetName() { return fName.c_str(); }
  98. void recurse(SkCanvas* canvas,
  99. int depth,
  100. const SkPoint& offset) {
  101. canvas->save();
  102. SkRect temp = SkRect::MakeLTRB(0, 0,
  103. fSizes[depth].fX, fSizes[depth].fY);
  104. temp.offset(offset);
  105. SkPath path;
  106. path.addRoundRect(temp, SkIntToScalar(3), SkIntToScalar(3));
  107. SkASSERT(path.isConvex());
  108. canvas->clipPath(path,
  109. 0 == depth ? kReplace_SkClipOp : kIntersect_SkClipOp,
  110. fDoAA);
  111. if (kNestingDepth == depth) {
  112. // we only draw the draw rect at the lowest nesting level
  113. SkPaint paint;
  114. paint.setColor(0xff000000 | fRandom.nextU());
  115. canvas->drawRect(fDrawRect, paint);
  116. } else {
  117. SkPoint childOffset = offset;
  118. this->recurse(canvas, depth+1, childOffset);
  119. childOffset += fSizes[depth+1];
  120. this->recurse(canvas, depth+1, childOffset);
  121. childOffset.fX = offset.fX + fSizes[depth+1].fX;
  122. childOffset.fY = offset.fY;
  123. this->recurse(canvas, depth+1, childOffset);
  124. childOffset.fX = offset.fX;
  125. childOffset.fY = offset.fY + fSizes[depth+1].fY;
  126. this->recurse(canvas, depth+1, childOffset);
  127. }
  128. canvas->restore();
  129. }
  130. virtual void onDraw(int loops, SkCanvas* canvas) {
  131. for (int i = 0; i < loops; ++i) {
  132. SkPoint offset = SkPoint::Make(0, 0);
  133. this->recurse(canvas, 0, offset);
  134. }
  135. }
  136. private:
  137. typedef Benchmark INHERITED;
  138. };
  139. ////////////////////////////////////////////////////////////////////////////////
  140. class AAClipBuilderBench : public Benchmark {
  141. SkString fName;
  142. SkPath fPath;
  143. SkRect fRect;
  144. SkRegion fRegion;
  145. bool fDoPath;
  146. bool fDoAA;
  147. public:
  148. AAClipBuilderBench(bool doPath, bool doAA) {
  149. fDoPath = doPath;
  150. fDoAA = doAA;
  151. fName.printf("aaclip_build_%s_%s", doPath ? "path" : "rect",
  152. doAA ? "AA" : "BW");
  153. fRegion.setRect(0, 0, 640, 480);
  154. fRect.set(fRegion.getBounds());
  155. fRect.inset(SK_Scalar1/4, SK_Scalar1/4);
  156. fPath.addRoundRect(fRect, SkIntToScalar(20), SkIntToScalar(20));
  157. }
  158. protected:
  159. virtual const char* onGetName() { return fName.c_str(); }
  160. virtual void onDraw(int loops, SkCanvas*) {
  161. SkPaint paint;
  162. this->setupPaint(&paint);
  163. for (int i = 0; i < loops; ++i) {
  164. SkAAClip clip;
  165. if (fDoPath) {
  166. clip.setPath(fPath, &fRegion, fDoAA);
  167. } else {
  168. clip.setRect(fRect, fDoAA);
  169. }
  170. }
  171. }
  172. private:
  173. typedef Benchmark INHERITED;
  174. };
  175. ////////////////////////////////////////////////////////////////////////////////
  176. class AAClipRegionBench : public Benchmark {
  177. public:
  178. AAClipRegionBench() {
  179. SkPath path;
  180. // test conversion of a complex clip to a aaclip
  181. path.addCircle(0, 0, SkIntToScalar(200));
  182. path.addCircle(0, 0, SkIntToScalar(180));
  183. // evenodd means we've constructed basically a stroked circle
  184. path.setFillType(SkPath::kEvenOdd_FillType);
  185. SkIRect bounds;
  186. path.getBounds().roundOut(&bounds);
  187. fRegion.setPath(path, SkRegion(bounds));
  188. }
  189. protected:
  190. virtual const char* onGetName() { return "aaclip_setregion"; }
  191. virtual void onDraw(int loops, SkCanvas*) {
  192. for (int i = 0; i < loops; ++i) {
  193. SkAAClip clip;
  194. clip.setRegion(fRegion);
  195. }
  196. }
  197. private:
  198. SkRegion fRegion;
  199. typedef Benchmark INHERITED;
  200. };
  201. ////////////////////////////////////////////////////////////////////////////////
  202. DEF_BENCH(return new AAClipBuilderBench(false, false);)
  203. DEF_BENCH(return new AAClipBuilderBench(false, true);)
  204. DEF_BENCH(return new AAClipBuilderBench(true, false);)
  205. DEF_BENCH(return new AAClipBuilderBench(true, true);)
  206. DEF_BENCH(return new AAClipRegionBench();)
  207. DEF_BENCH(return new AAClipBench(false, false);)
  208. DEF_BENCH(return new AAClipBench(false, true);)
  209. DEF_BENCH(return new AAClipBench(true, false);)
  210. DEF_BENCH(return new AAClipBench(true, true);)
  211. DEF_BENCH(return new NestedAAClipBench(false);)
  212. DEF_BENCH(return new NestedAAClipBench(true);)