Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
ImageCacheBench.cpp
Go to the documentation of this file.
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"
10
11namespace {
12static void* gGlobalAddress;
13class TestKey : public SkResourceCache::Key {
14public:
15 intptr_t fValue;
16
17 TestKey(intptr_t value) : fValue(value) {
18 this->init(&gGlobalAddress, 0, sizeof(fValue));
19 }
20};
21struct TestRec : public SkResourceCache::Rec {
22 TestKey fKey;
23 intptr_t fValue;
24
25 TestRec(const TestKey& key, intptr_t value) : fKey(key), fValue(value) {}
26
27 const Key& getKey() const override { return fKey; }
28 size_t bytesUsed() const override { return sizeof(fKey) + sizeof(fValue); }
29 const char* getCategory() const override { return "imagecachebench-test"; }
30 SkDiscardableMemory* diagnostic_only_getDiscardable() const override { return nullptr; }
31
32 static bool Visitor(const SkResourceCache::Rec&, void*) {
33 return true;
34 }
35};
36} // namespace
37
38class ImageCacheBench : public Benchmark {
39 SkResourceCache fCache;
40
41 enum {
42 CACHE_COUNT = 500
43 };
44public:
45 ImageCacheBench() : fCache(CACHE_COUNT * 100) {}
46
48 for (int i = 0; i < CACHE_COUNT; ++i) {
49 fCache.add(new TestRec(TestKey(i), i));
50 }
51 }
52
53protected:
54 const char* onGetName() override {
55 return "imagecache";
56 }
57
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
71private:
72 using INHERITED = Benchmark;
73};
74
75///////////////////////////////////////////////////////////////////////////////
76
77DEF_BENCH( return new ImageCacheBench(); )
#define DEF_BENCH(code)
Definition Benchmark.h:20
TArray< uint32_t > Key
#define SkASSERT(cond)
Definition SkAssert.h:116
#define SkDEBUGCODE(...)
Definition SkDebug.h:23
const char * onGetName() override
void onDraw(int loops, SkCanvas *) override
size_t getTotalBytesUsed() const
bool find(const Key &, FindVisitor, void *context)
void(* Visitor)(const Rec &, void *context)
void add(Rec *, void *payload=nullptr)
uint8_t value
void init(void *nameSpace, uint64_t sharedID, size_t dataSize)
virtual const char * getCategory() const =0
virtual SkDiscardableMemory * diagnostic_only_getDiscardable() const
virtual size_t bytesUsed() const =0
virtual const Key & getKey() const =0
TestKey(int sharedID, int32_t data)
TestRec(int sharedID, int32_t data, int *flagPtr)