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