OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 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 // This tests a Gr class | 8 // This tests a Gr class |
9 #if SK_SUPPORT_GPU | 9 #if SK_SUPPORT_GPU |
10 | 10 |
11 #include "GrMemoryPool.h" | 11 #include "GrMemoryPool.h" |
12 #include "SkBenchmark.h" | 12 #include "SkBenchmark.h" |
13 #include "SkRandom.h" | 13 #include "SkRandom.h" |
14 #include "SkTDArray.h" | 14 #include "SkTDArray.h" |
15 #include "SkTemplates.h" | 15 #include "SkTemplates.h" |
16 | 16 |
17 // change this to 0 to compare GrMemoryPool to default new / delete | 17 // change this to 0 to compare GrMemoryPool to default new / delete |
18 #define OVERRIDE_NEW 1 | 18 #define OVERRIDE_NEW 1 |
19 | 19 |
20 struct A { | 20 struct A { |
21 int gStuff[10]; | 21 int gStuff[10]; |
22 #if OVERRIDE_NEW | 22 #if OVERRIDE_NEW |
23 void* operator new (size_t size) { return gPool.allocate(size); } | 23 void* operator new (size_t size) { return gBenchPool.allocate(size); } |
24 void operator delete (void* mem) { if (mem) { return gPool.release(mem); } } | 24 void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem)
; } } |
25 #endif | 25 #endif |
26 static GrMemoryPool gPool; | 26 static GrMemoryPool gBenchPool; |
27 }; | 27 }; |
28 GrMemoryPool A::gPool(10 * (1 << 10), 10 * (1 << 10)); | 28 GrMemoryPool A::gBenchPool(10 * (1 << 10), 10 * (1 << 10)); |
29 | 29 |
30 /** | 30 /** |
31 * This benchmark creates and deletes objects in stack order | 31 * This benchmark creates and deletes objects in stack order |
32 */ | 32 */ |
33 class GrMemoryPoolBenchStack : public SkBenchmark { | 33 class GrMemoryPoolBenchStack : public SkBenchmark { |
34 public: | 34 public: |
35 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { | 35 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
36 return backend == kNonRendering_Backend; | 36 return backend == kNonRendering_Backend; |
37 } | 37 } |
38 | 38 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
72 } | 72 } |
73 for (int i = 0; i < count; ++i) { | 73 for (int i = 0; i < count; ++i) { |
74 delete objects[i]; | 74 delete objects[i]; |
75 } | 75 } |
76 } | 76 } |
77 | 77 |
78 private: | 78 private: |
79 typedef SkBenchmark INHERITED; | 79 typedef SkBenchmark INHERITED; |
80 }; | 80 }; |
81 | 81 |
| 82 struct B { |
| 83 int gStuff[10]; |
| 84 #if OVERRIDE_NEW |
| 85 void* operator new (size_t size) { return gBenchPool.allocate(size); } |
| 86 void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem)
; } } |
| 87 #endif |
| 88 static GrMemoryPool gBenchPool; |
| 89 }; |
| 90 GrMemoryPool B::gBenchPool(10 * (1 << 10), 10 * (1 << 10)); |
| 91 |
82 /** | 92 /** |
83 * This benchmark creates objects and deletes them in random order | 93 * This benchmark creates objects and deletes them in random order |
84 */ | 94 */ |
85 class GrMemoryPoolBenchRandom : public SkBenchmark { | 95 class GrMemoryPoolBenchRandom : public SkBenchmark { |
86 public: | 96 public: |
87 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { | 97 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
88 return backend == kNonRendering_Backend; | 98 return backend == kNonRendering_Backend; |
89 } | 99 } |
90 | 100 |
91 protected: | 101 protected: |
92 virtual const char* onGetName() { | 102 virtual const char* onGetName() { |
93 return "grmemorypool_random"; | 103 return "grmemorypool_random"; |
94 } | 104 } |
95 | 105 |
96 virtual void onDraw(const int loops, SkCanvas*) { | 106 virtual void onDraw(const int loops, SkCanvas*) { |
97 SkRandom r; | 107 SkRandom r; |
98 enum { | 108 enum { |
99 kMaxObjects = 4 * (1 << 10), | 109 kMaxObjects = 4 * (1 << 10), |
100 }; | 110 }; |
101 SkAutoTDelete<A> objects[kMaxObjects]; | 111 SkAutoTDelete<B> objects[kMaxObjects]; |
102 | 112 |
103 for (int i = 0; i < loops; i++) { | 113 for (int i = 0; i < loops; i++) { |
104 uint32_t idx = r.nextRangeU(0, kMaxObjects-1); | 114 uint32_t idx = r.nextRangeU(0, kMaxObjects-1); |
105 if (NULL == objects[idx].get()) { | 115 if (NULL == objects[idx].get()) { |
106 objects[idx].reset(new A); | 116 objects[idx].reset(new B); |
107 } else { | 117 } else { |
108 objects[idx].free(); | 118 objects[idx].free(); |
109 } | 119 } |
110 } | 120 } |
111 } | 121 } |
112 | 122 |
113 private: | 123 private: |
114 typedef SkBenchmark INHERITED; | 124 typedef SkBenchmark INHERITED; |
115 }; | 125 }; |
116 | 126 |
| 127 struct C { |
| 128 int gStuff[10]; |
| 129 #if OVERRIDE_NEW |
| 130 void* operator new (size_t size) { return gBenchPool.allocate(size); } |
| 131 void operator delete (void* mem) { if (mem) { return gBenchPool.release(mem)
; } } |
| 132 #endif |
| 133 static GrMemoryPool gBenchPool; |
| 134 }; |
| 135 GrMemoryPool C::gBenchPool(10 * (1 << 10), 10 * (1 << 10)); |
| 136 |
117 /** | 137 /** |
118 * This benchmark creates objects and deletes them in queue order | 138 * This benchmark creates objects and deletes them in queue order |
119 */ | 139 */ |
120 class GrMemoryPoolBenchQueue : public SkBenchmark { | 140 class GrMemoryPoolBenchQueue : public SkBenchmark { |
121 enum { | 141 enum { |
122 M = 4 * (1 << 10), | 142 M = 4 * (1 << 10), |
123 }; | 143 }; |
124 public: | 144 public: |
125 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { | 145 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { |
126 return backend == kNonRendering_Backend; | 146 return backend == kNonRendering_Backend; |
127 } | 147 } |
128 | 148 |
129 protected: | 149 protected: |
130 virtual const char* onGetName() { | 150 virtual const char* onGetName() { |
131 return "grmemorypool_queue"; | 151 return "grmemorypool_queue"; |
132 } | 152 } |
133 | 153 |
134 virtual void onDraw(const int loops, SkCanvas*) { | 154 virtual void onDraw(const int loops, SkCanvas*) { |
135 SkRandom r; | 155 SkRandom r; |
136 A* objects[M]; | 156 C* objects[M]; |
137 for (int i = 0; i < loops; i++) { | 157 for (int i = 0; i < loops; i++) { |
138 uint32_t count = r.nextRangeU(0, M-1); | 158 uint32_t count = r.nextRangeU(0, M-1); |
139 for (uint32_t i = 0; i < count; i++) { | 159 for (uint32_t i = 0; i < count; i++) { |
140 objects[i] = new A; | 160 objects[i] = new C; |
141 } | 161 } |
142 for (uint32_t i = 0; i < count; i++) { | 162 for (uint32_t i = 0; i < count; i++) { |
143 delete objects[i]; | 163 delete objects[i]; |
144 } | 164 } |
145 } | 165 } |
146 } | 166 } |
147 | 167 |
148 private: | 168 private: |
149 typedef SkBenchmark INHERITED; | 169 typedef SkBenchmark INHERITED; |
150 }; | 170 }; |
151 | 171 |
152 /////////////////////////////////////////////////////////////////////////////// | 172 /////////////////////////////////////////////////////////////////////////////// |
153 | 173 |
154 DEF_BENCH( return new GrMemoryPoolBenchStack(); ) | 174 DEF_BENCH( return new GrMemoryPoolBenchStack(); ) |
155 DEF_BENCH( return new GrMemoryPoolBenchRandom(); ) | 175 DEF_BENCH( return new GrMemoryPoolBenchRandom(); ) |
156 DEF_BENCH( return new GrMemoryPoolBenchQueue(); ) | 176 DEF_BENCH( return new GrMemoryPoolBenchQueue(); ) |
157 | 177 |
158 #endif | 178 #endif |
OLD | NEW |