PicturePlaybackBench.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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/SkColor.h"
  10. #include "include/core/SkPaint.h"
  11. #include "include/core/SkPicture.h"
  12. #include "include/core/SkPictureRecorder.h"
  13. #include "include/core/SkPoint.h"
  14. #include "include/core/SkRect.h"
  15. #include "include/core/SkString.h"
  16. #include "include/utils/SkRandom.h"
  17. // This is designed to emulate about 4 screens of textual content
  18. ///////////////////////////////////////////////////////////////////////////////
  19. // Chrome draws into small tiles with impl-side painting.
  20. // This benchmark measures the relative performance of our bounding-box hierarchies,
  21. // both when querying tiles perfectly and when not.
  22. enum BBH { kNone, kRTree };
  23. enum Mode { kTiled, kRandom };
  24. class TiledPlaybackBench : public Benchmark {
  25. public:
  26. TiledPlaybackBench(BBH bbh, Mode mode) : fBBH(bbh), fMode(mode), fName("tiled_playback") {
  27. switch (fBBH) {
  28. case kNone: fName.append("_none" ); break;
  29. case kRTree: fName.append("_rtree" ); break;
  30. }
  31. switch (fMode) {
  32. case kTiled: fName.append("_tiled" ); break;
  33. case kRandom: fName.append("_random"); break;
  34. }
  35. }
  36. const char* onGetName() override { return fName.c_str(); }
  37. SkIPoint onGetSize() override { return SkIPoint::Make(1024,1024); }
  38. void onDelayedSetup() override {
  39. std::unique_ptr<SkBBHFactory> factory;
  40. switch (fBBH) {
  41. case kNone: break;
  42. case kRTree: factory.reset(new SkRTreeFactory); break;
  43. }
  44. SkPictureRecorder recorder;
  45. SkCanvas* canvas = recorder.beginRecording(1024, 1024, factory.get());
  46. SkRandom rand;
  47. for (int i = 0; i < 10000; i++) {
  48. SkScalar x = rand.nextRangeScalar(0, 1024),
  49. y = rand.nextRangeScalar(0, 1024),
  50. w = rand.nextRangeScalar(0, 128),
  51. h = rand.nextRangeScalar(0, 128);
  52. SkPaint paint;
  53. paint.setColor(rand.nextU());
  54. paint.setAlpha(0xFF);
  55. canvas->drawRect(SkRect::MakeXYWH(x,y,w,h), paint);
  56. }
  57. fPic = recorder.finishRecordingAsPicture();
  58. }
  59. void onDraw(int loops, SkCanvas* canvas) override {
  60. for (int i = 0; i < loops; i++) {
  61. // This inner loop guarantees we make the same choices for all bench variants.
  62. SkRandom rand;
  63. for (int j = 0; j < 10; j++) {
  64. SkScalar x = 0, y = 0;
  65. switch (fMode) {
  66. case kTiled: x = SkScalar(256 * rand.nextULessThan(4));
  67. y = SkScalar(256 * rand.nextULessThan(4));
  68. break;
  69. case kRandom: x = rand.nextRangeScalar(0, 768);
  70. y = rand.nextRangeScalar(0, 768);
  71. break;
  72. }
  73. SkAutoCanvasRestore ar(canvas, true/*save now*/);
  74. canvas->clipRect(SkRect::MakeXYWH(x,y,256,256));
  75. fPic->playback(canvas);
  76. }
  77. }
  78. }
  79. private:
  80. BBH fBBH;
  81. Mode fMode;
  82. SkString fName;
  83. sk_sp<SkPicture> fPic;
  84. };
  85. DEF_BENCH( return new TiledPlaybackBench(kNone, kRandom); )
  86. DEF_BENCH( return new TiledPlaybackBench(kNone, kTiled ); )
  87. DEF_BENCH( return new TiledPlaybackBench(kRTree, kRandom); )
  88. DEF_BENCH( return new TiledPlaybackBench(kRTree, kTiled ); )