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/SkResourceCache.h"
10 #include "src/core/SkYUVPlanesCache.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(YUVPlanesCache,reporter)31 DEF_TEST(YUVPlanesCache, reporter) {
32 SkResourceCache cache(1024);
33
34 SkYUVPlanesCache::Info yuvInfo;
35 for (int i = 0; i < SkYUVASizeInfo::kMaxCount; i++) {
36 yuvInfo.fSizeInfo.fSizes[i].fWidth = 20 * (i + 1);
37 yuvInfo.fSizeInfo.fSizes[i].fHeight = 10 * (i + 1);
38 yuvInfo.fSizeInfo.fWidthBytes[i] = 80 * (i + 1);
39 }
40
41 for (int i = 0; i < SkYUVAIndex::kIndexCount; ++i) {
42 yuvInfo.fYUVAIndices[i].fIndex = -1;
43 yuvInfo.fYUVAIndices[i].fChannel = SkColorChannel::kR;
44 }
45 yuvInfo.fColorSpace = kRec601_SkYUVColorSpace;
46
47 const uint32_t genID = 12345678;
48
49 SkCachedData* data = SkYUVPlanesCache::FindAndRef(genID, &yuvInfo, &cache);
50 REPORTER_ASSERT(reporter, nullptr == data);
51
52 size_t size = 256;
53 data = cache.newCachedData(size);
54 memset(data->writable_data(), 0xff, size);
55
56 SkYUVPlanesCache::Add(genID, data, &yuvInfo, &cache);
57 check_data(reporter, data, 2, kInCache, kLocked);
58
59 data->unref();
60 check_data(reporter, data, 1, kInCache, kUnlocked);
61
62 SkYUVPlanesCache::Info yuvInfoRead;
63 data = SkYUVPlanesCache::FindAndRef(genID, &yuvInfoRead, &cache);
64
65 REPORTER_ASSERT(reporter, data);
66 REPORTER_ASSERT(reporter, data->size() == size);
67 REPORTER_ASSERT(reporter, yuvInfo.fSizeInfo == yuvInfoRead.fSizeInfo);
68
69 for (int i = 0; i < SkYUVAIndex::kIndexCount; ++i) {
70 REPORTER_ASSERT(reporter, yuvInfo.fYUVAIndices[i] == yuvInfoRead.fYUVAIndices[i]);
71 }
72
73 REPORTER_ASSERT(reporter, yuvInfo.fColorSpace == yuvInfoRead.fColorSpace);
74
75 check_data(reporter, data, 2, kInCache, kLocked);
76
77 cache.purgeAll();
78 check_data(reporter, data, 1, kNotInCache, kLocked);
79 data->unref();
80 }
81