Debian Codegraph @

bench/PicturePlaybackBench.cpp

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