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; |
| 39 | SkBlurQuality quality = kLow_SkBlurQuality; |
| 40 | SkMask mask; |
| 41 | |
reed | 4dca7a8 | 2014-10-23 12:42:46 -0700 | [diff] [blame] | 42 | SkCachedData* data = SkMaskCache::FindAndRef(sigma, style, quality, rrect, &mask, &cache); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 43 | REPORTER_ASSERT(reporter, nullptr == data); |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 44 | |
| 45 | size_t size = 256; |
| 46 | data = cache.newCachedData(size); |
| 47 | memset(data->writable_data(), 0xff, size); |
| 48 | mask.fBounds.setXYWH(0, 0, 100, 100); |
| 49 | mask.fRowBytes = 100; |
| 50 | mask.fFormat = SkMask::kBW_Format; |
reed | 4dca7a8 | 2014-10-23 12:42:46 -0700 | [diff] [blame] | 51 | SkMaskCache::Add(sigma, style, quality, rrect, mask, data, &cache); |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 52 | check_data(reporter, data, 2, kInCache, kLocked); |
| 53 | |
| 54 | data->unref(); |
| 55 | check_data(reporter, data, 1, kInCache, kUnlocked); |
| 56 | |
| 57 | sk_bzero(&mask, sizeof(mask)); |
reed | 4dca7a8 | 2014-10-23 12:42:46 -0700 | [diff] [blame] | 58 | data = SkMaskCache::FindAndRef(sigma, style, quality, rrect, &mask, &cache); |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 59 | REPORTER_ASSERT(reporter, data); |
| 60 | REPORTER_ASSERT(reporter, data->size() == size); |
| 61 | REPORTER_ASSERT(reporter, mask.fBounds.top() == 0 && mask.fBounds.bottom() == 100); |
| 62 | REPORTER_ASSERT(reporter, data->data() == (const void*)mask.fImage); |
| 63 | check_data(reporter, data, 2, kInCache, kLocked); |
| 64 | |
| 65 | cache.purgeAll(); |
| 66 | check_data(reporter, data, 1, kNotInCache, kLocked); |
| 67 | data->unref(); |
| 68 | } |
| 69 | |
| 70 | DEF_TEST(RectsMaskCache, reporter) { |
| 71 | SkResourceCache cache(1024); |
| 72 | |
| 73 | SkScalar sigma = 0.8f; |
| 74 | SkRect rect = SkRect::MakeWH(100, 100); |
| 75 | SkRect rects[2] = {rect}; |
| 76 | SkBlurStyle style = kNormal_SkBlurStyle; |
reed | 4dca7a8 | 2014-10-23 12:42:46 -0700 | [diff] [blame] | 77 | SkBlurQuality quality = kLow_SkBlurQuality; |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 78 | SkMask mask; |
| 79 | |
reed | 4dca7a8 | 2014-10-23 12:42:46 -0700 | [diff] [blame] | 80 | SkCachedData* data = SkMaskCache::FindAndRef(sigma, style, quality, rects, 1, &mask, &cache); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 81 | REPORTER_ASSERT(reporter, nullptr == data); |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 82 | |
| 83 | size_t size = 256; |
| 84 | data = cache.newCachedData(size); |
| 85 | memset(data->writable_data(), 0xff, size); |
| 86 | mask.fBounds.setXYWH(0, 0, 100, 100); |
| 87 | mask.fRowBytes = 100; |
| 88 | mask.fFormat = SkMask::kBW_Format; |
reed | 4dca7a8 | 2014-10-23 12:42:46 -0700 | [diff] [blame] | 89 | SkMaskCache::Add(sigma, style, quality, rects, 1, mask, data, &cache); |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 90 | check_data(reporter, data, 2, kInCache, kLocked); |
| 91 | |
| 92 | data->unref(); |
| 93 | check_data(reporter, data, 1, kInCache, kUnlocked); |
| 94 | |
| 95 | sk_bzero(&mask, sizeof(mask)); |
reed | 4dca7a8 | 2014-10-23 12:42:46 -0700 | [diff] [blame] | 96 | data = SkMaskCache::FindAndRef(sigma, style, quality, rects, 1, &mask, &cache); |
qiankun.miao | d9aac34 | 2014-10-23 07:58:17 -0700 | [diff] [blame] | 97 | REPORTER_ASSERT(reporter, data); |
| 98 | REPORTER_ASSERT(reporter, data->size() == size); |
| 99 | REPORTER_ASSERT(reporter, mask.fBounds.top() == 0 && mask.fBounds.bottom() == 100); |
| 100 | REPORTER_ASSERT(reporter, data->data() == (const void*)mask.fImage); |
| 101 | check_data(reporter, data, 2, kInCache, kLocked); |
| 102 | |
| 103 | cache.purgeAll(); |
| 104 | check_data(reporter, data, 1, kNotInCache, kLocked); |
| 105 | data->unref(); |
| 106 | } |