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 "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 lockedSta
te) { |
| 25 REPORTER_ASSERT(reporter, data->testing_only_getRefCnt() == refcnt); |
| 26 REPORTER_ASSERT(reporter, data->testing_only_isInCache() == (kInCache == cac
heState)); |
| 27 bool isLocked = (data->data() != NULL); |
| 28 REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked)); |
| 29 } |
| 30 |
| 31 DEF_TEST(MaskCache, reporter) { |
| 32 |
| 33 SkResourceCache cache(1024); |
| 34 |
| 35 SkScalar sigma = 2; |
| 36 SkRect r = SkRect::MakeWH(10, 10); |
| 37 SkMask mask; |
| 38 |
| 39 SkCachedData* data = SkMaskCache::FindAndLock(sigma, r, &mask, &cache); |
| 40 REPORTER_ASSERT(reporter, NULL == data); |
| 41 |
| 42 size_t size = 100; |
| 43 data = cache.newCachedData(size); |
| 44 check_data(reporter, data, 1, kNotInCache, kLocked); |
| 45 data->ref(); |
| 46 check_data(reporter, data, 2, kNotInCache, kLocked); |
| 47 data->unref(); |
| 48 check_data(reporter, data, 1, kNotInCache, kLocked); |
| 49 |
| 50 memset(data->writable_data(), 0x80, size); // just to use writable_data() |
| 51 mask.fBounds.setXYWH(1, 2, 3, 4); |
| 52 SkMaskCache::Add(sigma, r, mask, data, &cache); |
| 53 check_data(reporter, data, 2, kInCache, kLocked); |
| 54 |
| 55 data->unref(); |
| 56 check_data(reporter, data, 1, kInCache, kUnlocked); |
| 57 |
| 58 sk_bzero(&mask, sizeof(mask)); |
| 59 data = SkMaskCache::FindAndLock(sigma, r, &mask, &cache); |
| 60 check_data(reporter, data, 2, kInCache, kLocked); |
| 61 |
| 62 REPORTER_ASSERT(reporter, data); |
| 63 REPORTER_ASSERT(reporter, data->size() == size); |
| 64 REPORTER_ASSERT(reporter, mask.fBounds.top() == 2); |
| 65 REPORTER_ASSERT(reporter, data->data() == (const void*)mask.fImage); |
| 66 |
| 67 cache.purgeAll(); |
| 68 check_data(reporter, data, 1, kNotInCache, kLocked); |
| 69 data->unref(); |
| 70 } |
| 71 |
OLD | NEW |