blob: f4d88da0424d75a0c11f9f9495ee63f8dbbde59d [file] [log] [blame]
reed@google.com602a1d72013-07-23 19:13:54 +00001/*
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
tfarinaf168b862014-06-19 12:32:29 -07008#include "Benchmark.h"
reed@google.com602a1d72013-07-23 19:13:54 +00009#include "SkScaledImageCache.h"
10
reed04617132014-08-21 09:46:49 -070011namespace {
12static void* gGlobalAddress;
13class TestKey : public SkScaledImageCache::Key {
14public:
15 void* fPtr;
16 intptr_t fValue;
17
18 TestKey(intptr_t value) : fPtr(&gGlobalAddress), fValue(value) {
19 this->init(sizeof(fPtr) + sizeof(fValue));
20 }
21};
reed680fb9e2014-08-26 09:08:04 -070022struct TestRec : public SkScaledImageCache::Rec {
23 TestKey fKey;
24 intptr_t fValue;
25
26 TestRec(const TestKey& key, intptr_t value) : fKey(key), fValue(value) {}
27
28 virtual const Key& getKey() const SK_OVERRIDE { return fKey; }
29 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + sizeof(fValue); }
30};
reed04617132014-08-21 09:46:49 -070031}
32
tfarinaf168b862014-06-19 12:32:29 -070033class ImageCacheBench : public Benchmark {
reed@google.com602a1d72013-07-23 19:13:54 +000034 SkScaledImageCache fCache;
35 SkBitmap fBM;
36
37 enum {
reed@google.com602a1d72013-07-23 19:13:54 +000038 DIM = 1,
39 CACHE_COUNT = 500
40 };
41public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000042 ImageCacheBench() : fCache(CACHE_COUNT * 100) {
reed6c225732014-06-09 19:52:07 -070043 fBM.allocN32Pixels(DIM, DIM);
reed@google.com602a1d72013-07-23 19:13:54 +000044 }
45
46 void populateCache() {
reed@google.com602a1d72013-07-23 19:13:54 +000047 for (int i = 0; i < CACHE_COUNT; ++i) {
reed680fb9e2014-08-26 09:08:04 -070048 fCache.unlock(fCache.addAndLock(SkNEW_ARGS(TestRec, (TestKey(i), i))));
reed@google.com602a1d72013-07-23 19:13:54 +000049 }
50 }
51
52protected:
53 virtual const char* onGetName() SK_OVERRIDE {
54 return "imagecache";
55 }
56
commit-bot@chromium.org33614712013-12-03 18:17:16 +000057 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
halcanary805ef152014-07-17 06:58:01 -070058 if (fCache.getTotalBytesUsed() == 0) {
reed@google.com602a1d72013-07-23 19:13:54 +000059 this->populateCache();
60 }
61
reed04617132014-08-21 09:46:49 -070062 TestKey key(-1);
reed680fb9e2014-08-26 09:08:04 -070063 // search for a miss (-1)
commit-bot@chromium.org33614712013-12-03 18:17:16 +000064 for (int i = 0; i < loops; ++i) {
reed680fb9e2014-08-26 09:08:04 -070065 SkDEBUGCODE(SkScaledImageCache::ID id =) fCache.findAndLock(key);
reed04617132014-08-21 09:46:49 -070066 SkASSERT(NULL == id);
reed@google.com602a1d72013-07-23 19:13:54 +000067 }
68 }
69
70private:
tfarinaf168b862014-06-19 12:32:29 -070071 typedef Benchmark INHERITED;
reed@google.com602a1d72013-07-23 19:13:54 +000072};
73
74///////////////////////////////////////////////////////////////////////////////
75
mtklein@google.com410e6e82013-09-13 19:52:27 +000076DEF_BENCH( return new ImageCacheBench(); )