blob: 07f332baa22d5cb5e923ac625ce086a9e0c158fc [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};
22}
23
tfarinaf168b862014-06-19 12:32:29 -070024class ImageCacheBench : public Benchmark {
reed@google.com602a1d72013-07-23 19:13:54 +000025 SkScaledImageCache fCache;
26 SkBitmap fBM;
27
28 enum {
reed@google.com602a1d72013-07-23 19:13:54 +000029 DIM = 1,
30 CACHE_COUNT = 500
31 };
32public:
mtklein@google.com410e6e82013-09-13 19:52:27 +000033 ImageCacheBench() : fCache(CACHE_COUNT * 100) {
reed6c225732014-06-09 19:52:07 -070034 fBM.allocN32Pixels(DIM, DIM);
reed@google.com602a1d72013-07-23 19:13:54 +000035 }
36
37 void populateCache() {
reed@google.com602a1d72013-07-23 19:13:54 +000038 for (int i = 0; i < CACHE_COUNT; ++i) {
reed04617132014-08-21 09:46:49 -070039 TestKey key(i);
reed@google.com602a1d72013-07-23 19:13:54 +000040 SkBitmap tmp;
reed6c225732014-06-09 19:52:07 -070041 tmp.allocN32Pixels(1, 1);
reed04617132014-08-21 09:46:49 -070042 fCache.unlock(fCache.addAndLock(key, tmp));
reed@google.com602a1d72013-07-23 19:13:54 +000043 }
44 }
45
46protected:
47 virtual const char* onGetName() SK_OVERRIDE {
48 return "imagecache";
49 }
50
commit-bot@chromium.org33614712013-12-03 18:17:16 +000051 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
halcanary805ef152014-07-17 06:58:01 -070052 if (fCache.getTotalBytesUsed() == 0) {
reed@google.com602a1d72013-07-23 19:13:54 +000053 this->populateCache();
54 }
55
reed04617132014-08-21 09:46:49 -070056 TestKey key(-1);
reed@google.com602a1d72013-07-23 19:13:54 +000057 SkBitmap tmp;
58 // search for a miss (-1 scale)
commit-bot@chromium.org33614712013-12-03 18:17:16 +000059 for (int i = 0; i < loops; ++i) {
reed04617132014-08-21 09:46:49 -070060 SkDEBUGCODE(SkScaledImageCache::ID* id =) fCache.findAndLock(key, &tmp);
61 SkASSERT(NULL == id);
reed@google.com602a1d72013-07-23 19:13:54 +000062 }
63 }
64
65private:
tfarinaf168b862014-06-19 12:32:29 -070066 typedef Benchmark INHERITED;
reed@google.com602a1d72013-07-23 19:13:54 +000067};
68
69///////////////////////////////////////////////////////////////////////////////
70
mtklein@google.com410e6e82013-09-13 19:52:27 +000071DEF_BENCH( return new ImageCacheBench(); )