liyujie
2025-08-28 786ff4f4ca2374bdd9177f2e24b503d43e7a3b93
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
 
#include "Benchmark.h"
#include "SkResourceCache.h"
 
namespace {
static void* gGlobalAddress;
class TestKey : public SkResourceCache::Key {
public:
    intptr_t fValue;
 
    TestKey(intptr_t value) : fValue(value) {
        this->init(&gGlobalAddress, 0, sizeof(fValue));
    }
};
struct TestRec : public SkResourceCache::Rec {
    TestKey     fKey;
    intptr_t    fValue;
 
    TestRec(const TestKey& key, intptr_t value) : fKey(key), fValue(value) {}
 
    const Key& getKey() const override { return fKey; }
    size_t bytesUsed() const override { return sizeof(fKey) + sizeof(fValue); }
    const char* getCategory() const override { return "imagecachebench-test"; }
    SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; }
 
    static bool Visitor(const SkResourceCache::Rec&, void*) {
        return true;
    }
};
}
 
class ImageCacheBench : public Benchmark {
    SkResourceCache fCache;
 
    enum {
        CACHE_COUNT = 500
    };
public:
    ImageCacheBench()  : fCache(CACHE_COUNT * 100) {}
 
    void populateCache() {
        for (int i = 0; i < CACHE_COUNT; ++i) {
            fCache.add(new TestRec(TestKey(i), i));
        }
    }
 
protected:
    const char* onGetName() override {
        return "imagecache";
    }
 
    void onDraw(int loops, SkCanvas*) override {
        if (fCache.getTotalBytesUsed() == 0) {
            this->populateCache();
        }
 
        TestKey key(-1);
        // search for a miss (-1)
        for (int i = 0; i < loops; ++i) {
            SkDEBUGCODE(bool found =) fCache.find(key, TestRec::Visitor, nullptr);
            SkASSERT(!found);
        }
    }
 
private:
    typedef Benchmark INHERITED;
};
 
///////////////////////////////////////////////////////////////////////////////
 
DEF_BENCH( return new ImageCacheBench(); )