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