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