OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2014 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #include "SkCachedData.h" |
| 9 #include "SkDiscardableMemoryPool.h" |
| 10 #include "Test.h" |
| 11 |
| 12 enum LockedState { |
| 13 kUnlocked, |
| 14 kLocked, |
| 15 }; |
| 16 |
| 17 enum CachedState { |
| 18 kNotInCache, |
| 19 kInCache, |
| 20 }; |
| 21 |
| 22 static void check_data(skiatest::Reporter* reporter, SkCachedData* data, |
| 23 int refcnt, CachedState cacheState, LockedState lockedSta
te) { |
| 24 REPORTER_ASSERT(reporter, data->testing_only_getRefCnt() == refcnt); |
| 25 REPORTER_ASSERT(reporter, data->testing_only_isInCache() == (kInCache == cac
heState)); |
| 26 bool isLocked = (data->data() != NULL); |
| 27 REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked)); |
| 28 } |
| 29 |
| 30 static SkCachedData* make_data(size_t size, SkDiscardableMemoryPool* pool) { |
| 31 if (pool) { |
| 32 SkDiscardableMemory* dm = pool->create(size); |
| 33 // the pool "can" return null, but it shouldn't in these controlled cond
itions |
| 34 SK_ALWAYSBREAK(dm); |
| 35 return SkNEW_ARGS(SkCachedData, (size, dm)); |
| 36 } else { |
| 37 return SkNEW_ARGS(SkCachedData, (sk_malloc_throw(size), size)); |
| 38 } |
| 39 } |
| 40 |
| 41 // returns with the data locked by client and cache |
| 42 static SkCachedData* test_locking(skiatest::Reporter* reporter, |
| 43 size_t size, SkDiscardableMemoryPool* pool) { |
| 44 SkCachedData* data = make_data(size, pool); |
| 45 |
| 46 memset(data->writable_data(), 0x80, size); // just to use writable_data() |
| 47 |
| 48 check_data(reporter, data, 1, kNotInCache, kLocked); |
| 49 |
| 50 data->ref(); |
| 51 check_data(reporter, data, 2, kNotInCache, kLocked); |
| 52 data->unref(); |
| 53 check_data(reporter, data, 1, kNotInCache, kLocked); |
| 54 |
| 55 data->attachToCacheAndRef(); |
| 56 check_data(reporter, data, 2, kInCache, kLocked); |
| 57 |
| 58 data->unref(); |
| 59 check_data(reporter, data, 1, kInCache, kUnlocked); |
| 60 |
| 61 data->ref(); |
| 62 check_data(reporter, data, 2, kInCache, kLocked); |
| 63 |
| 64 return data; |
| 65 } |
| 66 |
| 67 /* |
| 68 * SkCachedData behaves differently (regarding its locked/unlocked state) depen
ding on |
| 69 * when it is in the cache or not. Being in the cache is signaled by calling at
tachToCacheAndRef() |
| 70 * instead of ref(). (and balanced by detachFromCacheAndUnref). |
| 71 * |
| 72 * Thus, among other things, we test the end-of-life behavior when the client i
s the last owner |
| 73 * and when the cache is. |
| 74 */ |
| 75 DEF_TEST(CachedData, reporter) { |
| 76 SkAutoTUnref<SkDiscardableMemoryPool> pool(SkDiscardableMemoryPool::Create(1
000)); |
| 77 |
| 78 for (int useDiscardable = 0; useDiscardable <= 1; ++useDiscardable) { |
| 79 const size_t size = 100; |
| 80 |
| 81 // test with client as last owner |
| 82 SkCachedData* data = test_locking(reporter, size, useDiscardable ? pool.
get() : NULL); |
| 83 check_data(reporter, data, 2, kInCache, kLocked); |
| 84 data->detachFromCacheAndUnref(); |
| 85 check_data(reporter, data, 1, kNotInCache, kLocked); |
| 86 data->unref(); |
| 87 |
| 88 // test with cache as last owner |
| 89 data = test_locking(reporter, size, useDiscardable ? pool.get() : NULL); |
| 90 check_data(reporter, data, 2, kInCache, kLocked); |
| 91 data->unref(); |
| 92 check_data(reporter, data, 1, kInCache, kUnlocked); |
| 93 data->detachFromCacheAndUnref(); |
| 94 } |
| 95 } |
| 96 |
OLD | NEW |