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