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 "src/core/SkCachedData.h"
9 #include "src/core/SkMaskCache.h"
10 #include "src/core/SkResourceCache.h"
11 #include "tests/Test.h"
12
13 enum LockedState {
14 kUnlocked,
15 kLocked,
16 };
17
18 enum CachedState {
19 kNotInCache,
20 kInCache,
21 };
22
check_data(skiatest::Reporter * reporter,SkCachedData * data,int refcnt,CachedState cacheState,LockedState lockedState)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));
27 bool isLocked = (data->data() != nullptr);
28 REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked));
29 }
30
DEF_TEST(RRectMaskCache,reporter)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 SkMask mask;
40
41 SkCachedData* data = SkMaskCache::FindAndRef(sigma, style, rrect, &mask, &cache);
42 REPORTER_ASSERT(reporter, nullptr == data);
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;
50 SkMaskCache::Add(sigma, style, rrect, mask, data, &cache);
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));
57 data = SkMaskCache::FindAndRef(sigma, style, rrect, &mask, &cache);
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
DEF_TEST(RectsMaskCache,reporter)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
78 SkCachedData* data = SkMaskCache::FindAndRef(sigma, style, rects, 1, &mask, &cache);
79 REPORTER_ASSERT(reporter, nullptr == data);
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;
87 SkMaskCache::Add(sigma, style, rects, 1, mask, data, &cache);
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));
94 data = SkMaskCache::FindAndRef(sigma, style, rects, 1, &mask, &cache);
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 }
105