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 | |
| 8 | #include "SkBenchmark.h" |
| 9 | #include "SkScaledImageCache.h" |
| 10 | |
| 11 | class ImageCacheBench : public SkBenchmark { |
| 12 | SkScaledImageCache fCache; |
| 13 | SkBitmap fBM; |
| 14 | |
| 15 | enum { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 16 | DIM = 1, |
| 17 | CACHE_COUNT = 500 |
| 18 | }; |
| 19 | public: |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 20 | ImageCacheBench() : fCache(CACHE_COUNT * 100) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 21 | fBM.setConfig(SkBitmap::kARGB_8888_Config, DIM, DIM); |
| 22 | fBM.allocPixels(); |
| 23 | } |
| 24 | |
| 25 | void populateCache() { |
| 26 | SkScalar scale = 1; |
| 27 | for (int i = 0; i < CACHE_COUNT; ++i) { |
| 28 | SkBitmap tmp; |
| 29 | tmp.setConfig(SkBitmap::kARGB_8888_Config, 1, 1); |
| 30 | tmp.allocPixels(); |
| 31 | fCache.unlock(fCache.addAndLock(fBM, scale, scale, tmp)); |
| 32 | scale += 1; |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | protected: |
| 37 | virtual const char* onGetName() SK_OVERRIDE { |
| 38 | return "imagecache"; |
| 39 | } |
| 40 | |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 41 | virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 42 | if (fCache.getBytesUsed() == 0) { |
| 43 | this->populateCache(); |
| 44 | } |
| 45 | |
| 46 | SkBitmap tmp; |
| 47 | // search for a miss (-1 scale) |
commit-bot@chromium.org | 3361471 | 2013-12-03 18:17:16 +0000 | [diff] [blame] | 48 | for (int i = 0; i < loops; ++i) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 49 | (void)fCache.findAndLock(fBM, -1, -1, &tmp); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | private: |
| 54 | typedef SkBenchmark INHERITED; |
| 55 | }; |
| 56 | |
| 57 | /////////////////////////////////////////////////////////////////////////////// |
| 58 | |
mtklein@google.com | 410e6e8 | 2013-09-13 19:52:27 +0000 | [diff] [blame] | 59 | DEF_BENCH( return new ImageCacheBench(); ) |