qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -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 "SkMaskCache.h" |
| 10 | #include "SkResourceCache.h" |
| 11 | #include "Test.h" |
| 12 | |
| 13 | enum LockedState { |
| 14 | kUnlocked, |
| 15 | kLocked, |
| 16 | }; |
| 17 | |
| 18 | enum CachedState { |
| 19 | kNotInCache, |
| 20 | kInCache, |
| 21 | }; |
| 22 | |
| 23 | static void check_data(skiatest::Reporter* reporter, SkCachedData* data, |
| 24 | int refcnt, CachedState cacheState, LockedState lockedState) { |
| 25 | REPORTER_ASSERT(reporter, data->testing_only_getRefCnt() == refcnt); |
| 26 | REPORTER_ASSERT(reporter, data->testing_only_isInCache() == (kInCache == cacheState)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 27 | bool isLocked = (data->data() != nullptr); |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 28 | REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked)); |
| 29 | } |
| 30 | |
| 31 | DEF_TEST(RRectMaskCache, reporter) { |
| 32 | SkResourceCache cache(1024); |
| 33 | |
| 34 | SkScalar sigma = 0.8f; |
| 35 | SkRect rect = SkRect::MakeWH(100, 100); |
| 36 | SkRRect rrect; |
| 37 | rrect.setRectXY(rect, 30, 30); |
| 38 | SkBlurStyle style = kNormal_SkBlurStyle; |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 39 | SkMask mask; |
| 40 | |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame^] | 41 | SkCachedData* data = SkMaskCache::FindAndRef(sigma, style, rrect, &mask, &cache); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 42 | REPORTER_ASSERT(reporter, nullptr == data); |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 43 | |
| 44 | size_t size = 256; |
| 45 | data = cache.newCachedData(size); |
| 46 | memset(data->writable_data(), 0xff, size); |
| 47 | mask.fBounds.setXYWH(0, 0, 100, 100); |
| 48 | mask.fRowBytes = 100; |
| 49 | mask.fFormat = SkMask::kBW_Format; |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame^] | 50 | SkMaskCache::Add(sigma, style, rrect, mask, data, &cache); |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 51 | check_data(reporter, data, 2, kInCache, kLocked); |
| 52 | |
| 53 | data->unref(); |
| 54 | check_data(reporter, data, 1, kInCache, kUnlocked); |
| 55 | |
| 56 | sk_bzero(&mask, sizeof(mask)); |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame^] | 57 | data = SkMaskCache::FindAndRef(sigma, style, rrect, &mask, &cache); |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 58 | REPORTER_ASSERT(reporter, data); |
| 59 | REPORTER_ASSERT(reporter, data->size() == size); |
| 60 | REPORTER_ASSERT(reporter, mask.fBounds.top() == 0 && mask.fBounds.bottom() == 100); |
| 61 | REPORTER_ASSERT(reporter, data->data() == (const void*)mask.fImage); |
| 62 | check_data(reporter, data, 2, kInCache, kLocked); |
| 63 | |
| 64 | cache.purgeAll(); |
| 65 | check_data(reporter, data, 1, kNotInCache, kLocked); |
| 66 | data->unref(); |
| 67 | } |
| 68 | |
| 69 | DEF_TEST(RectsMaskCache, reporter) { |
| 70 | SkResourceCache cache(1024); |
| 71 | |
| 72 | SkScalar sigma = 0.8f; |
| 73 | SkRect rect = SkRect::MakeWH(100, 100); |
| 74 | SkRect rects[2] = {rect}; |
| 75 | SkBlurStyle style = kNormal_SkBlurStyle; |
| 76 | SkMask mask; |
| 77 | |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame^] | 78 | SkCachedData* data = SkMaskCache::FindAndRef(sigma, style, rects, 1, &mask, &cache); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 79 | REPORTER_ASSERT(reporter, nullptr == data); |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 80 | |
| 81 | size_t size = 256; |
| 82 | data = cache.newCachedData(size); |
| 83 | memset(data->writable_data(), 0xff, size); |
| 84 | mask.fBounds.setXYWH(0, 0, 100, 100); |
| 85 | mask.fRowBytes = 100; |
| 86 | mask.fFormat = SkMask::kBW_Format; |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame^] | 87 | SkMaskCache::Add(sigma, style, rects, 1, mask, data, &cache); |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 88 | check_data(reporter, data, 2, kInCache, kLocked); |
| 89 | |
| 90 | data->unref(); |
| 91 | check_data(reporter, data, 1, kInCache, kUnlocked); |
| 92 | |
| 93 | sk_bzero(&mask, sizeof(mask)); |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame^] | 94 | data = SkMaskCache::FindAndRef(sigma, style, rects, 1, &mask, &cache); |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 95 | REPORTER_ASSERT(reporter, data); |
| 96 | REPORTER_ASSERT(reporter, data->size() == size); |
| 97 | REPORTER_ASSERT(reporter, mask.fBounds.top() == 0 && mask.fBounds.bottom() == 100); |
| 98 | REPORTER_ASSERT(reporter, data->data() == (const void*)mask.fImage); |
| 99 | check_data(reporter, data, 2, kInCache, kLocked); |
| 100 | |
| 101 | cache.purgeAll(); |
| 102 | check_data(reporter, data, 1, kNotInCache, kLocked); |
| 103 | data->unref(); |
| 104 | } |