reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 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 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 8 | #include "Benchmark.h" |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 9 | #include "SkScaledImageCache.h" |
| 10 | |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 11 | namespace { |
| 12 | static void* gGlobalAddress; |
| 13 | class TestKey : public SkScaledImageCache::Key { |
| 14 | public: |
| 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 | }; |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 22 | struct 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 | }; |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 31 | } |
| 32 | |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 33 | class ImageCacheBench : public Benchmark { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 34 | SkScaledImageCache fCache; |
| 35 | SkBitmap fBM; |
| 36 | |
| 37 | enum { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 38 | DIM = 1, |
| 39 | CACHE_COUNT = 500 |
| 40 | }; |
| 41 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 42 | ImageCacheBench() : fCache(CACHE_COUNT * 100) { |
reed | 6c22573 | 2014-06-09 19:52:07 -0700 | [diff] [blame] | 43 | fBM.allocN32Pixels(DIM, DIM); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void populateCache() { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 47 | for (int i = 0; i < CACHE_COUNT; ++i) { |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 48 | fCache.unlock(fCache.addAndLock(SkNEW_ARGS(TestRec, (TestKey(i), i)))); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | |
| 52 | protected: |
| 53 | virtual const char* onGetName() SK_OVERRIDE { |
| 54 | return "imagecache"; |
| 55 | } |
| 56 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 57 | virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE { |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 58 | if (fCache.getTotalBytesUsed() == 0) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 59 | this->populateCache(); |
| 60 | } |
| 61 | |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 62 | TestKey key(-1); |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 63 | // search for a miss (-1) |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 64 | for (int i = 0; i < loops; ++i) { |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 65 | SkDEBUGCODE(SkScaledImageCache::ID id =) fCache.findAndLock(key); |
reed | 0461713 | 2014-08-21 09:46:49 -0700 | [diff] [blame] | 66 | SkASSERT(NULL == id); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | private: |
tfarina | f168b86 | 2014-06-19 12:32:29 -0700 | [diff] [blame] | 71 | typedef Benchmark INHERITED; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | /////////////////////////////////////////////////////////////////////////////// |
| 75 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 76 | DEF_BENCH( return new ImageCacheBench(); ) |