reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 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 lockedState) { |
| 24 | REPORTER_ASSERT(reporter, data->testing_only_getRefCnt() == refcnt); |
| 25 | REPORTER_ASSERT(reporter, data->testing_only_isInCache() == (kInCache == cacheState)); |
| 26 | REPORTER_ASSERT(reporter, data->testing_only_isLocked() == (lockedState == kLocked)); |
| 27 | } |
| 28 | |
| 29 | static SkCachedData* make_data(size_t size, SkDiscardableMemoryPool* pool) { |
| 30 | if (pool) { |
| 31 | SkDiscardableMemory* dm = pool->create(size); |
| 32 | // the pool "can" return null, but it shouldn't in these controlled conditions |
djsollen | f2b340f | 2016-01-29 08:51:04 -0800 | [diff] [blame] | 33 | SkASSERT_RELEASE(dm); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 34 | return new SkCachedData(size, dm); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 35 | } else { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 36 | return new SkCachedData(sk_malloc_throw(size), size); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
| 40 | // returns with the data locked by client and cache |
| 41 | static SkCachedData* test_locking(skiatest::Reporter* reporter, |
| 42 | size_t size, SkDiscardableMemoryPool* pool) { |
| 43 | SkCachedData* data = make_data(size, pool); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 44 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 45 | memset(data->writable_data(), 0x80, size); // just to use writable_data() |
| 46 | |
| 47 | check_data(reporter, data, 1, kNotInCache, kLocked); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 48 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 49 | data->ref(); |
| 50 | check_data(reporter, data, 2, kNotInCache, kLocked); |
| 51 | data->unref(); |
| 52 | check_data(reporter, data, 1, kNotInCache, kLocked); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 53 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 54 | data->attachToCacheAndRef(); |
| 55 | check_data(reporter, data, 2, kInCache, kLocked); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 56 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 57 | data->unref(); |
| 58 | check_data(reporter, data, 1, kInCache, kUnlocked); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 59 | |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 60 | data->ref(); |
| 61 | check_data(reporter, data, 2, kInCache, kLocked); |
| 62 | |
| 63 | return data; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * SkCachedData behaves differently (regarding its locked/unlocked state) depending on |
| 68 | * when it is in the cache or not. Being in the cache is signaled by calling attachToCacheAndRef() |
| 69 | * instead of ref(). (and balanced by detachFromCacheAndUnref). |
| 70 | * |
| 71 | * Thus, among other things, we test the end-of-life behavior when the client is the last owner |
| 72 | * and when the cache is. |
| 73 | */ |
| 74 | DEF_TEST(CachedData, reporter) { |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 75 | sk_sp<SkDiscardableMemoryPool> pool(SkDiscardableMemoryPool::Create(1000)); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 76 | |
| 77 | for (int useDiscardable = 0; useDiscardable <= 1; ++useDiscardable) { |
| 78 | const size_t size = 100; |
| 79 | |
| 80 | // test with client as last owner |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 81 | SkCachedData* data = test_locking(reporter, size, useDiscardable ? pool.get() : nullptr); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 82 | check_data(reporter, data, 2, kInCache, kLocked); |
| 83 | data->detachFromCacheAndUnref(); |
| 84 | check_data(reporter, data, 1, kNotInCache, kLocked); |
| 85 | data->unref(); |
| 86 | |
| 87 | // test with cache as last owner |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 88 | data = test_locking(reporter, size, useDiscardable ? pool.get() : nullptr); |
reed | 9d93c2e | 2014-10-08 05:17:12 -0700 | [diff] [blame] | 89 | check_data(reporter, data, 2, kInCache, kLocked); |
| 90 | data->unref(); |
| 91 | check_data(reporter, data, 1, kInCache, kUnlocked); |
| 92 | data->detachFromCacheAndUnref(); |
| 93 | } |
| 94 | } |