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 { | 11 namespace { |
12 static void* gGlobalAddress; | 12 static void* gGlobalAddress; |
13 class TestKey : public SkScaledImageCache::Key { | 13 class TestKey : public SkScaledImageCache::Key { |
14 public: | 14 public: |
15 void* fPtr; | 15 void* fPtr; |
16 intptr_t fValue; | 16 intptr_t fValue; |
17 | 17 |
18 TestKey(intptr_t value) : fPtr(&gGlobalAddress), fValue(value) { | 18 TestKey(intptr_t value) : fPtr(&gGlobalAddress), fValue(value) { |
19 this->init(sizeof(fPtr) + sizeof(fValue)); | 19 this->init(sizeof(fPtr) + sizeof(fValue)); |
20 } | 20 } |
21 }; | 21 }; |
| 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 }; |
22 } | 31 } |
23 | 32 |
24 class ImageCacheBench : public Benchmark { | 33 class ImageCacheBench : public Benchmark { |
25 SkScaledImageCache fCache; | 34 SkScaledImageCache fCache; |
26 SkBitmap fBM; | 35 SkBitmap fBM; |
27 | 36 |
28 enum { | 37 enum { |
29 DIM = 1, | 38 DIM = 1, |
30 CACHE_COUNT = 500 | 39 CACHE_COUNT = 500 |
31 }; | 40 }; |
32 public: | 41 public: |
33 ImageCacheBench() : fCache(CACHE_COUNT * 100) { | 42 ImageCacheBench() : fCache(CACHE_COUNT * 100) { |
34 fBM.allocN32Pixels(DIM, DIM); | 43 fBM.allocN32Pixels(DIM, DIM); |
35 } | 44 } |
36 | 45 |
37 void populateCache() { | 46 void populateCache() { |
38 for (int i = 0; i < CACHE_COUNT; ++i) { | 47 for (int i = 0; i < CACHE_COUNT; ++i) { |
39 TestKey key(i); | 48 fCache.unlock(fCache.addAndLock(SkNEW_ARGS(TestRec, (TestKey(i), i))
)); |
40 SkBitmap tmp; | |
41 tmp.allocN32Pixels(1, 1); | |
42 fCache.unlock(fCache.addAndLock(key, tmp)); | |
43 } | 49 } |
44 } | 50 } |
45 | 51 |
46 protected: | 52 protected: |
47 virtual const char* onGetName() SK_OVERRIDE { | 53 virtual const char* onGetName() SK_OVERRIDE { |
48 return "imagecache"; | 54 return "imagecache"; |
49 } | 55 } |
50 | 56 |
51 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE { | 57 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE { |
52 if (fCache.getTotalBytesUsed() == 0) { | 58 if (fCache.getTotalBytesUsed() == 0) { |
53 this->populateCache(); | 59 this->populateCache(); |
54 } | 60 } |
55 | 61 |
56 TestKey key(-1); | 62 TestKey key(-1); |
57 SkBitmap tmp; | 63 // search for a miss (-1) |
58 // search for a miss (-1 scale) | |
59 for (int i = 0; i < loops; ++i) { | 64 for (int i = 0; i < loops; ++i) { |
60 SkDEBUGCODE(SkScaledImageCache::ID* id =) fCache.findAndLock(key, &t
mp); | 65 SkDEBUGCODE(SkScaledImageCache::ID id =) fCache.findAndLock(key); |
61 SkASSERT(NULL == id); | 66 SkASSERT(NULL == id); |
62 } | 67 } |
63 } | 68 } |
64 | 69 |
65 private: | 70 private: |
66 typedef Benchmark INHERITED; | 71 typedef Benchmark INHERITED; |
67 }; | 72 }; |
68 | 73 |
69 /////////////////////////////////////////////////////////////////////////////// | 74 /////////////////////////////////////////////////////////////////////////////// |
70 | 75 |
71 DEF_BENCH( return new ImageCacheBench(); ) | 76 DEF_BENCH( return new ImageCacheBench(); ) |
OLD | NEW |