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 here to ensure SK_SUPPORT_GPU is set correctly before it is examined. | 8 // Include here to ensure SK_SUPPORT_GPU is set correctly before it is examined. |
9 #include "SkTypes.h" | 9 #include "SkTypes.h" |
10 | 10 |
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 static const size_t kDefaultSize = 100; | 243 static const size_t kDefaultSize = 100; |
244 | 244 |
245 /** Property that distinctly categorizes the resource. | 245 /** Property that distinctly categorizes the resource. |
246 * For example, textures have width, height, ... */ | 246 * For example, textures have width, height, ... */ |
247 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty }; | 247 enum SimulatedProperty { kA_SimulatedProperty, kB_SimulatedProperty }; |
248 | 248 |
249 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle) | 249 TestResource(GrGpu* gpu, size_t size, GrGpuResource::LifeCycle lifeCycle) |
250 : INHERITED(gpu, lifeCycle) | 250 : INHERITED(gpu, lifeCycle) |
251 , fToDelete(nullptr) | 251 , fToDelete(nullptr) |
252 , fSize(size) | 252 , fSize(size) |
253 , fProperty(kA_SimulatedProperty) { | 253 , fProperty(kA_SimulatedProperty) |
| 254 , fReuseable(true) { |
254 ++fNumAlive; | 255 ++fNumAlive; |
255 this->registerWithCache(); | 256 this->registerWithCache(); |
256 } | 257 } |
257 | 258 |
258 TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle) | 259 TestResource(GrGpu* gpu, GrGpuResource::LifeCycle lifeCycle) |
259 : INHERITED(gpu, lifeCycle) | 260 : INHERITED(gpu, lifeCycle) |
260 , fToDelete(nullptr) | 261 , fToDelete(nullptr) |
261 , fSize(kDefaultSize) | 262 , fSize(kDefaultSize) |
262 , fProperty(kA_SimulatedProperty) { | 263 , fProperty(kA_SimulatedProperty) |
| 264 , fReuseable(true) { |
263 ++fNumAlive; | 265 ++fNumAlive; |
264 this->registerWithCache(); | 266 this->registerWithCache(); |
265 } | 267 } |
266 | 268 |
267 TestResource(GrGpu* gpu) | 269 TestResource(GrGpu* gpu) |
268 : INHERITED(gpu, kCached_LifeCycle) | 270 : INHERITED(gpu, kCached_LifeCycle) |
269 , fToDelete(nullptr) | 271 , fToDelete(nullptr) |
270 , fSize(kDefaultSize) | 272 , fSize(kDefaultSize) |
271 , fProperty(kA_SimulatedProperty) { | 273 , fProperty(kA_SimulatedProperty) |
| 274 , fReuseable(true) { |
272 ++fNumAlive; | 275 ++fNumAlive; |
273 this->registerWithCache(); | 276 this->registerWithCache(); |
274 } | 277 } |
275 | 278 |
276 static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, b
ool cached = true) { | 279 static TestResource* CreateScratch(GrGpu* gpu, SimulatedProperty property, b
ool cached = true, |
277 return new TestResource(gpu, property, cached, kScratchConstructor); | 280 bool reuseable = true) { |
| 281 return new TestResource(gpu, property, cached, kScratchConstructor, reus
eable); |
278 } | 282 } |
279 | 283 |
280 ~TestResource() { | 284 ~TestResource() { |
281 --fNumAlive; | 285 --fNumAlive; |
282 SkSafeUnref(fToDelete); | 286 SkSafeUnref(fToDelete); |
283 } | 287 } |
284 | 288 |
285 void setSize(size_t size) { | 289 void setSize(size_t size) { |
286 fSize = size; | 290 fSize = size; |
287 this->didChangeGpuMemorySize(); | 291 this->didChangeGpuMemorySize(); |
(...skipping 13 matching lines...) Expand all Loading... |
301 } | 305 } |
302 } | 306 } |
303 | 307 |
304 static size_t ExpectedScratchKeySize() { | 308 static size_t ExpectedScratchKeySize() { |
305 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaData
Cnt); | 309 return sizeof(uint32_t) * (kScratchKeyFieldCnt + GrScratchKey::kMetaData
Cnt); |
306 } | 310 } |
307 | 311 |
308 private: | 312 private: |
309 static const int kScratchKeyFieldCnt = 6; | 313 static const int kScratchKeyFieldCnt = 6; |
310 | 314 |
311 TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchCon
structor) | 315 TestResource(GrGpu* gpu, SimulatedProperty property, bool cached, ScratchCon
structor, |
| 316 bool reuseable) |
312 : INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle) | 317 : INHERITED(gpu, cached ? kCached_LifeCycle : kUncached_LifeCycle) |
313 , fToDelete(nullptr) | 318 , fToDelete(nullptr) |
314 , fSize(kDefaultSize) | 319 , fSize(kDefaultSize) |
315 , fProperty(property) { | 320 , fProperty(property) |
| 321 , fReuseable(reuseable) { |
316 GrScratchKey scratchKey; | 322 GrScratchKey scratchKey; |
317 ComputeScratchKey(fProperty, &scratchKey); | 323 ComputeScratchKey(fProperty, &scratchKey); |
318 this->setScratchKey(scratchKey); | 324 this->setScratchKey(scratchKey); |
319 ++fNumAlive; | 325 ++fNumAlive; |
320 this->registerWithCache(); | 326 this->registerWithCache(); |
321 } | 327 } |
322 | 328 |
323 size_t onGpuMemorySize() const override { return fSize; } | 329 size_t onGpuMemorySize() const override { return fSize; } |
| 330 bool reuseable() const override { return fReuseable; } |
324 | 331 |
325 TestResource* fToDelete; | 332 TestResource* fToDelete; |
326 size_t fSize; | 333 size_t fSize; |
327 static int fNumAlive; | 334 static int fNumAlive; |
328 SimulatedProperty fProperty; | 335 SimulatedProperty fProperty; |
| 336 bool fReuseable; |
329 typedef GrGpuResource INHERITED; | 337 typedef GrGpuResource INHERITED; |
330 }; | 338 }; |
331 int TestResource::fNumAlive = 0; | 339 int TestResource::fNumAlive = 0; |
332 | 340 |
333 class Mock { | 341 class Mock { |
334 public: | 342 public: |
335 Mock(int maxCnt, size_t maxBytes) { | 343 Mock(int maxCnt, size_t maxBytes) { |
336 fContext.reset(GrContext::CreateMockContext()); | 344 fContext.reset(GrContext::CreateMockContext()); |
337 SkASSERT(fContext); | 345 SkASSERT(fContext); |
338 fContext->setResourceCacheLimits(maxCnt, maxBytes); | 346 fContext->setResourceCacheLimits(maxCnt, maxBytes); |
(...skipping 842 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1181 cache->notifyFlushOccurred(); | 1189 cache->notifyFlushOccurred(); |
1182 } | 1190 } |
1183 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); | 1191 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
1184 | 1192 |
1185 cache->purgeAllUnlocked(); | 1193 cache->purgeAllUnlocked(); |
1186 } | 1194 } |
1187 | 1195 |
1188 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); | 1196 REPORTER_ASSERT(reporter, 0 == cache->getResourceCount()); |
1189 } | 1197 } |
1190 | 1198 |
| 1199 static void test_reuseable_resources(skiatest::Reporter* reporter) { |
| 1200 Mock mock(1000, 1000); |
| 1201 GrContext* context = mock.context(); |
| 1202 TestResource* scratch = TestResource::CreateScratch(context->getGpu(), |
| 1203 TestResource::kB_SimulatedProperty, true, true); |
| 1204 scratch->unref(); |
| 1205 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 1); |
| 1206 scratch = TestResource::CreateScratch(context->getGpu(), |
| 1207 TestResource::kB_SimulatedProperty, false, true); |
| 1208 scratch->unref(); |
| 1209 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2); |
| 1210 scratch = TestResource::CreateScratch(context->getGpu(), |
| 1211 TestResource::kB_SimulatedProperty, false, false); |
| 1212 scratch->unref(); |
| 1213 REPORTER_ASSERT(reporter, TestResource::NumAlive() == 2); |
| 1214 } |
| 1215 |
1191 static void test_large_resource_count(skiatest::Reporter* reporter) { | 1216 static void test_large_resource_count(skiatest::Reporter* reporter) { |
1192 // Set the cache size to double the resource count because we're going to cr
eate 2x that number | 1217 // Set the cache size to double the resource count because we're going to cr
eate 2x that number |
1193 // resources, using two different key domains. Add a little slop to the byte
s because we resize | 1218 // resources, using two different key domains. Add a little slop to the byte
s because we resize |
1194 // down to 1 byte after creating the resource. | 1219 // down to 1 byte after creating the resource. |
1195 static const int kResourceCnt = 2000; | 1220 static const int kResourceCnt = 2000; |
1196 | 1221 |
1197 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000); | 1222 Mock mock(2 * kResourceCnt, 2 * kResourceCnt + 1000); |
1198 GrContext* context = mock.context(); | 1223 GrContext* context = mock.context(); |
1199 GrResourceCache* cache = mock.cache(); | 1224 GrResourceCache* cache = mock.cache(); |
1200 | 1225 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1302 test_unbudgeted_to_scratch(reporter); | 1327 test_unbudgeted_to_scratch(reporter); |
1303 test_duplicate_unique_key(reporter); | 1328 test_duplicate_unique_key(reporter); |
1304 test_duplicate_scratch_key(reporter); | 1329 test_duplicate_scratch_key(reporter); |
1305 test_remove_scratch_key(reporter); | 1330 test_remove_scratch_key(reporter); |
1306 test_scratch_key_consistency(reporter); | 1331 test_scratch_key_consistency(reporter); |
1307 test_purge_invalidated(reporter); | 1332 test_purge_invalidated(reporter); |
1308 test_cache_chained_purge(reporter); | 1333 test_cache_chained_purge(reporter); |
1309 test_resource_size_changed(reporter); | 1334 test_resource_size_changed(reporter); |
1310 test_timestamp_wrap(reporter); | 1335 test_timestamp_wrap(reporter); |
1311 test_flush(reporter); | 1336 test_flush(reporter); |
| 1337 test_reuseable_resources(reporter); |
1312 test_large_resource_count(reporter); | 1338 test_large_resource_count(reporter); |
1313 test_custom_data(reporter); | 1339 test_custom_data(reporter); |
1314 test_abandoned(reporter); | 1340 test_abandoned(reporter); |
1315 } | 1341 } |
1316 | 1342 |
1317 #endif | 1343 #endif |
OLD | NEW |