| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "Benchmark.h" | 8 #include "Benchmark.h" |
| 9 #include "SkScaledImageCache.h" | 9 #include "SkScaledImageCache.h" |
| 10 | 10 |
| 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 |
| 11 class ImageCacheBench : public Benchmark { | 24 class ImageCacheBench : public Benchmark { |
| 12 SkScaledImageCache fCache; | 25 SkScaledImageCache fCache; |
| 13 SkBitmap fBM; | 26 SkBitmap fBM; |
| 14 | 27 |
| 15 enum { | 28 enum { |
| 16 DIM = 1, | 29 DIM = 1, |
| 17 CACHE_COUNT = 500 | 30 CACHE_COUNT = 500 |
| 18 }; | 31 }; |
| 19 public: | 32 public: |
| 20 ImageCacheBench() : fCache(CACHE_COUNT * 100) { | 33 ImageCacheBench() : fCache(CACHE_COUNT * 100) { |
| 21 fBM.allocN32Pixels(DIM, DIM); | 34 fBM.allocN32Pixels(DIM, DIM); |
| 22 } | 35 } |
| 23 | 36 |
| 24 void populateCache() { | 37 void populateCache() { |
| 25 SkScalar scale = 1; | |
| 26 for (int i = 0; i < CACHE_COUNT; ++i) { | 38 for (int i = 0; i < CACHE_COUNT; ++i) { |
| 39 TestKey key(i); |
| 27 SkBitmap tmp; | 40 SkBitmap tmp; |
| 28 tmp.allocN32Pixels(1, 1); | 41 tmp.allocN32Pixels(1, 1); |
| 29 fCache.unlock(fCache.addAndLock(fBM, scale, scale, tmp)); | 42 fCache.unlock(fCache.addAndLock(key, tmp)); |
| 30 scale += 1; | |
| 31 } | 43 } |
| 32 } | 44 } |
| 33 | 45 |
| 34 protected: | 46 protected: |
| 35 virtual const char* onGetName() SK_OVERRIDE { | 47 virtual const char* onGetName() SK_OVERRIDE { |
| 36 return "imagecache"; | 48 return "imagecache"; |
| 37 } | 49 } |
| 38 | 50 |
| 39 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE { | 51 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE { |
| 40 if (fCache.getTotalBytesUsed() == 0) { | 52 if (fCache.getTotalBytesUsed() == 0) { |
| 41 this->populateCache(); | 53 this->populateCache(); |
| 42 } | 54 } |
| 43 | 55 |
| 56 TestKey key(-1); |
| 44 SkBitmap tmp; | 57 SkBitmap tmp; |
| 45 // search for a miss (-1 scale) | 58 // search for a miss (-1 scale) |
| 46 for (int i = 0; i < loops; ++i) { | 59 for (int i = 0; i < loops; ++i) { |
| 47 (void)fCache.findAndLock(fBM, -1, -1, &tmp); | 60 SkDEBUGCODE(SkScaledImageCache::ID* id =) fCache.findAndLock(key, &t
mp); |
| 61 SkASSERT(NULL == id); |
| 48 } | 62 } |
| 49 } | 63 } |
| 50 | 64 |
| 51 private: | 65 private: |
| 52 typedef Benchmark INHERITED; | 66 typedef Benchmark INHERITED; |
| 53 }; | 67 }; |
| 54 | 68 |
| 55 /////////////////////////////////////////////////////////////////////////////// | 69 /////////////////////////////////////////////////////////////////////////////// |
| 56 | 70 |
| 57 DEF_BENCH( return new ImageCacheBench(); ) | 71 DEF_BENCH( return new ImageCacheBench(); ) |
| OLD | NEW |