• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 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 "src/core/SkResourceCache.h"
10 
11 namespace {
12 static void* gGlobalAddress;
13 class TestKey : public SkResourceCache::Key {
14 public:
15     intptr_t fValue;
16 
TestKey(intptr_t value)17     TestKey(intptr_t value) : fValue(value) {
18         this->init(&gGlobalAddress, 0, sizeof(fValue));
19     }
20 };
21 struct TestRec : public SkResourceCache::Rec {
22     TestKey     fKey;
23     intptr_t    fValue;
24 
TestRec__anon199500aa0111::TestRec25     TestRec(const TestKey& key, intptr_t value) : fKey(key), fValue(value) {}
26 
getKey__anon199500aa0111::TestRec27     const Key& getKey() const override { return fKey; }
bytesUsed__anon199500aa0111::TestRec28     size_t bytesUsed() const override { return sizeof(fKey) + sizeof(fValue); }
getCategory__anon199500aa0111::TestRec29     const char* getCategory() const override { return "imagecachebench-test"; }
diagnostic_only_getDiscardable__anon199500aa0111::TestRec30     SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; }
31 
Visitor__anon199500aa0111::TestRec32     static bool Visitor(const SkResourceCache::Rec&, void*) {
33         return true;
34     }
35 };
36 }
37 
38 class ImageCacheBench : public Benchmark {
39     SkResourceCache fCache;
40 
41     enum {
42         CACHE_COUNT = 500
43     };
44 public:
ImageCacheBench()45     ImageCacheBench()  : fCache(CACHE_COUNT * 100) {}
46 
populateCache()47     void populateCache() {
48         for (int i = 0; i < CACHE_COUNT; ++i) {
49             fCache.add(new TestRec(TestKey(i), i));
50         }
51     }
52 
53 protected:
onGetName()54     const char* onGetName() override {
55         return "imagecache";
56     }
57 
onDraw(int loops,SkCanvas *)58     void onDraw(int loops, SkCanvas*) override {
59         if (fCache.getTotalBytesUsed() == 0) {
60             this->populateCache();
61         }
62 
63         TestKey key(-1);
64         // search for a miss (-1)
65         for (int i = 0; i < loops; ++i) {
66             SkDEBUGCODE(bool found =) fCache.find(key, TestRec::Visitor, nullptr);
67             SkASSERT(!found);
68         }
69     }
70 
71 private:
72     typedef Benchmark INHERITED;
73 };
74 
75 ///////////////////////////////////////////////////////////////////////////////
76 
77 DEF_BENCH( return new ImageCacheBench(); )
78