djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 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 | */ |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 9 | #include "bench/Benchmark.h" |
| 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkPaint.h" |
| 13 | #include "include/core/SkPicture.h" |
| 14 | #include "include/core/SkPictureRecorder.h" |
| 15 | #include "include/core/SkPoint.h" |
| 16 | #include "include/core/SkRect.h" |
| 17 | #include "include/core/SkString.h" |
| 18 | #include "include/utils/SkRandom.h" |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 19 | |
| 20 | // This is designed to emulate about 4 screens of textual content |
| 21 | |
djsollen@google.com | 809a2a9 | 2012-02-23 20:57:09 +0000 | [diff] [blame] | 22 | /////////////////////////////////////////////////////////////////////////////// |
| 23 | |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 24 | // Chrome draws into small tiles with impl-side painting. |
| 25 | // This benchmark measures the relative performance of our bounding-box hierarchies, |
| 26 | // both when querying tiles perfectly and when not. |
mtklein | 703dd2e | 2015-01-09 06:41:48 -0800 | [diff] [blame] | 27 | enum BBH { kNone, kRTree }; |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 28 | enum Mode { kTiled, kRandom }; |
| 29 | class TiledPlaybackBench : public Benchmark { |
| 30 | public: |
| 31 | TiledPlaybackBench(BBH bbh, Mode mode) : fBBH(bbh), fMode(mode), fName("tiled_playback") { |
| 32 | switch (fBBH) { |
| 33 | case kNone: fName.append("_none" ); break; |
| 34 | case kRTree: fName.append("_rtree" ); break; |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 35 | } |
| 36 | switch (fMode) { |
| 37 | case kTiled: fName.append("_tiled" ); break; |
| 38 | case kRandom: fName.append("_random"); break; |
| 39 | } |
| 40 | } |
| 41 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 42 | const char* onGetName() override { return fName.c_str(); } |
| 43 | SkIPoint onGetSize() override { return SkIPoint::Make(1024,1024); } |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 44 | |
joshualitt | 8a6697a | 2015-09-30 12:11:07 -0700 | [diff] [blame] | 45 | void onDelayedSetup() override { |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 46 | std::unique_ptr<SkBBHFactory> factory; |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 47 | switch (fBBH) { |
John Stiles | fbd050b | 2020-08-03 13:21:46 -0400 | [diff] [blame] | 48 | case kNone: break; |
| 49 | case kRTree: factory = std::make_unique<SkRTreeFactory>(); break; |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | SkPictureRecorder recorder; |
Ben Wagner | 145dbcd | 2016-11-03 14:40:50 -0400 | [diff] [blame] | 53 | SkCanvas* canvas = recorder.beginRecording(1024, 1024, factory.get()); |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 54 | SkRandom rand; |
| 55 | for (int i = 0; i < 10000; i++) { |
| 56 | SkScalar x = rand.nextRangeScalar(0, 1024), |
| 57 | y = rand.nextRangeScalar(0, 1024), |
| 58 | w = rand.nextRangeScalar(0, 128), |
| 59 | h = rand.nextRangeScalar(0, 128); |
| 60 | SkPaint paint; |
| 61 | paint.setColor(rand.nextU()); |
| 62 | paint.setAlpha(0xFF); |
| 63 | canvas->drawRect(SkRect::MakeXYWH(x,y,w,h), paint); |
| 64 | } |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 65 | fPic = recorder.finishRecordingAsPicture(); |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 66 | } |
| 67 | |
mtklein | a1ebeb2 | 2015-10-01 09:43:39 -0700 | [diff] [blame] | 68 | void onDraw(int loops, SkCanvas* canvas) override { |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 69 | for (int i = 0; i < loops; i++) { |
| 70 | // This inner loop guarantees we make the same choices for all bench variants. |
| 71 | SkRandom rand; |
| 72 | for (int j = 0; j < 10; j++) { |
| 73 | SkScalar x = 0, y = 0; |
| 74 | switch (fMode) { |
| 75 | case kTiled: x = SkScalar(256 * rand.nextULessThan(4)); |
| 76 | y = SkScalar(256 * rand.nextULessThan(4)); |
| 77 | break; |
| 78 | case kRandom: x = rand.nextRangeScalar(0, 768); |
| 79 | y = rand.nextRangeScalar(0, 768); |
| 80 | break; |
| 81 | } |
| 82 | SkAutoCanvasRestore ar(canvas, true/*save now*/); |
| 83 | canvas->clipRect(SkRect::MakeXYWH(x,y,256,256)); |
| 84 | fPic->playback(canvas); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private: |
reed | ca2622b | 2016-03-18 07:25:55 -0700 | [diff] [blame] | 90 | BBH fBBH; |
| 91 | Mode fMode; |
| 92 | SkString fName; |
| 93 | sk_sp<SkPicture> fPic; |
mtklein | bf5dd41 | 2014-11-11 10:39:27 -0800 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | DEF_BENCH( return new TiledPlaybackBench(kNone, kRandom); ) |
| 97 | DEF_BENCH( return new TiledPlaybackBench(kNone, kTiled ); ) |
| 98 | DEF_BENCH( return new TiledPlaybackBench(kRTree, kRandom); ) |
| 99 | DEF_BENCH( return new TiledPlaybackBench(kRTree, kTiled ); ) |