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 "SkResourceCache.h" | 9 #include "SkResourceCache.h" |
10 | 10 |
11 namespace { | 11 namespace { |
12 static void* gGlobalAddress; | 12 static void* gGlobalAddress; |
13 class TestKey : public SkResourceCache::Key { | 13 class TestKey : public SkResourceCache::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 SkResourceCache::Rec { | 22 struct TestRec : public SkResourceCache::Rec { |
23 TestKey fKey; | 23 TestKey fKey; |
24 intptr_t fValue; | 24 intptr_t fValue; |
25 | 25 |
26 TestRec(const TestKey& key, intptr_t value) : fKey(key), fValue(value) {} | 26 TestRec(const TestKey& key, intptr_t value) : fKey(key), fValue(value) {} |
27 | 27 |
28 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } | 28 virtual const Key& getKey() const SK_OVERRIDE { return fKey; } |
29 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + sizeof(
fValue); } | 29 virtual size_t bytesUsed() const SK_OVERRIDE { return sizeof(fKey) + sizeof(
fValue); } |
30 | |
31 static bool Visitor(const SkResourceCache::Rec&, void*) { | |
32 return true; | |
33 } | |
34 }; | 30 }; |
35 } | 31 } |
36 | 32 |
37 class ImageCacheBench : public Benchmark { | 33 class ImageCacheBench : public Benchmark { |
38 SkResourceCache fCache; | 34 SkResourceCache fCache; |
39 | 35 |
40 enum { | 36 enum { |
41 CACHE_COUNT = 500 | 37 CACHE_COUNT = 500 |
42 }; | 38 }; |
43 public: | 39 public: |
44 ImageCacheBench() : fCache(CACHE_COUNT * 100) {} | 40 ImageCacheBench() : fCache(CACHE_COUNT * 100) {} |
45 | 41 |
46 void populateCache() { | 42 void populateCache() { |
47 for (int i = 0; i < CACHE_COUNT; ++i) { | 43 for (int i = 0; i < CACHE_COUNT; ++i) { |
48 fCache.add(SkNEW_ARGS(TestRec, (TestKey(i), i))); | 44 fCache.unlock(fCache.addAndLock(SkNEW_ARGS(TestRec, (TestKey(i), i))
)); |
49 } | 45 } |
50 } | 46 } |
51 | 47 |
52 protected: | 48 protected: |
53 virtual const char* onGetName() SK_OVERRIDE { | 49 virtual const char* onGetName() SK_OVERRIDE { |
54 return "imagecache"; | 50 return "imagecache"; |
55 } | 51 } |
56 | 52 |
57 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE { | 53 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE { |
58 if (fCache.getTotalBytesUsed() == 0) { | 54 if (fCache.getTotalBytesUsed() == 0) { |
59 this->populateCache(); | 55 this->populateCache(); |
60 } | 56 } |
61 | 57 |
62 TestKey key(-1); | 58 TestKey key(-1); |
63 // search for a miss (-1) | 59 // search for a miss (-1) |
64 for (int i = 0; i < loops; ++i) { | 60 for (int i = 0; i < loops; ++i) { |
65 SkDEBUGCODE(bool found =) fCache.find(key, TestRec::Visitor, NULL); | 61 SkDEBUGCODE(SkResourceCache::ID id =) fCache.findAndLock(key); |
66 SkASSERT(!found); | 62 SkASSERT(NULL == id); |
67 } | 63 } |
68 } | 64 } |
69 | 65 |
70 private: | 66 private: |
71 typedef Benchmark INHERITED; | 67 typedef Benchmark INHERITED; |
72 }; | 68 }; |
73 | 69 |
74 /////////////////////////////////////////////////////////////////////////////// | 70 /////////////////////////////////////////////////////////////////////////////// |
75 | 71 |
76 DEF_BENCH( return new ImageCacheBench(); ) | 72 DEF_BENCH( return new ImageCacheBench(); ) |
OLD | NEW |