xref: /aosp_15_r20/external/skia/bench/TopoSortBench.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2015 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 
8 #include "bench/Benchmark.h"
9 #include "include/core/SkString.h"
10 #include "src/base/SkRandom.h"
11 #include "src/gpu/ganesh/GrTTopoSort.h"
12 
13 #include "tools/ToolUtils.h"
14 
15 using namespace skia_private;
16 
17 class TopoSortBench : public Benchmark {
18 public:
TopoSortBench()19     TopoSortBench() { }
20 
~TopoSortBench()21     ~TopoSortBench() override {
22     }
23 
isSuitableFor(Backend backend)24     bool isSuitableFor(Backend backend) override {
25         return Backend::kNonRendering == backend;
26     }
27 
28 protected:
onGetName()29     const char* onGetName() override {
30         return "sort_topo_rand";
31     }
32 
33     // Delayed initialization only done if onDraw will be called.
onDelayedSetup()34     void onDelayedSetup() override {
35         ToolUtils::TopoTestNode::AllocNodes(&fGraph, kNumElements);
36 
37         for (int i = kNumElements-1; i > 0; --i) {
38             int numEdges = fRand.nextU() % (kMaxEdges+1);
39 
40             for (int j = 0; j < numEdges; ++j) {
41                 int dep = fRand.nextU() % i;
42 
43                 fGraph[i]->dependsOn(fGraph[dep].get());
44             }
45         }
46     }
47 
onDraw(int loops,SkCanvas *)48     void onDraw(int loops, SkCanvas*) override {
49         for (int i = 0; i < loops; ++i) {
50             for (int j = 0; j < fGraph.size(); ++j) {
51                 fGraph[j]->reset();
52             }
53 
54             ToolUtils::TopoTestNode::Shuffle(fGraph, &fRand);
55 
56             SkDEBUGCODE(bool actualResult =) GrTTopoSort<ToolUtils::TopoTestNode>(fGraph);
57             SkASSERT(actualResult);
58 
59 #ifdef SK_DEBUG
60             for (int j = 0; j < fGraph.size(); ++j) {
61                 SkASSERT(fGraph[j]->check());
62             }
63 #endif
64         }
65     }
66 
67 private:
68     static const int kNumElements = 1000;
69     static const int kMaxEdges = 5;
70 
71     TArray<sk_sp<ToolUtils::TopoTestNode>> fGraph;
72     SkRandom fRand;
73 
74     using INHERITED = Benchmark;
75 };
76 
77 ///////////////////////////////////////////////////////////////////////////////
78 
79 DEF_BENCH( return new TopoSortBench(); )
80