• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012 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/private/SkChecksum.h"
10 #include "include/private/base/SkTemplates.h"
11 #include "src/base/SkRandom.h"
12 #include "src/core/SkMD5.h"
13 #include "src/core/SkOpts.h"
14 
15 enum ChecksumType {
16     kMD5_ChecksumType,
17     kHash_ChecksumType,
18 };
19 
20 class ComputeChecksumBench : public Benchmark {
21     enum {
22         U32COUNT  = 256,
23         SIZE      = U32COUNT * 4,
24     };
25     uint32_t    fData[U32COUNT];
26     ChecksumType fType;
27 
28 public:
ComputeChecksumBench(ChecksumType type)29     ComputeChecksumBench(ChecksumType type) : fType(type) {
30         SkRandom rand;
31         for (int i = 0; i < U32COUNT; ++i) {
32             fData[i] = rand.nextU();
33         }
34     }
35 
isSuitableFor(Backend backend)36     bool isSuitableFor(Backend backend) override {
37         return backend == kNonRendering_Backend;
38     }
39 
40 protected:
onGetName()41     const char* onGetName() override {
42         switch (fType) {
43             case kMD5_ChecksumType: return "compute_md5";
44             case kHash_ChecksumType: return "compute_hash";
45 
46             default: SK_ABORT("Invalid Type");
47         }
48     }
49 
onDraw(int loops,SkCanvas *)50     void onDraw(int loops, SkCanvas*) override {
51         switch (fType) {
52             case kMD5_ChecksumType: {
53                 for (int i = 0; i < loops; i++) {
54                     SkMD5 md5;
55                     md5.write(fData, sizeof(fData));
56                     (void)md5.finish();
57                 }
58             } break;
59             case kHash_ChecksumType: {
60                 for (int i = 0; i < loops; i++) {
61                     volatile uint32_t result = SkOpts::hash(fData, sizeof(fData));
62                     sk_ignore_unused_variable(result);
63                 }
64             }break;
65         }
66 
67     }
68 
69 private:
70     using INHERITED = Benchmark;
71 };
72 
73 ///////////////////////////////////////////////////////////////////////////////
74 
75 DEF_BENCH( return new ComputeChecksumBench(kMD5_ChecksumType); )
76 DEF_BENCH( return new ComputeChecksumBench(kHash_ChecksumType); )
77