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 "include/core/SkYUVAInfo.h"
9 #include "include/core/SkYUVAPixmaps.h"
10 #include "src/core/SkCachedData.h"
11 #include "src/core/SkResourceCache.h"
12 #include "src/core/SkYUVPlanesCache.h"
13 #include "tests/Test.h"
14
15 enum LockedState {
16 kUnlocked,
17 kLocked,
18 };
19
20 enum CachedState {
21 kNotInCache,
22 kInCache,
23 };
24
check_data(skiatest::Reporter * reporter,SkCachedData * data,int refcnt,CachedState cacheState,LockedState lockedState)25 static void check_data(skiatest::Reporter* reporter, SkCachedData* data,
26 int refcnt, CachedState cacheState, LockedState lockedState) {
27 REPORTER_ASSERT(reporter, data->testing_only_getRefCnt() == refcnt);
28 REPORTER_ASSERT(reporter, data->testing_only_isInCache() == (kInCache == cacheState));
29 bool isLocked = (data->data() != nullptr);
30 REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked));
31 }
32
DEF_TEST(YUVPlanesCache,reporter)33 DEF_TEST(YUVPlanesCache, reporter) {
34 SkResourceCache cache(1024);
35
36 SkYUVAInfo yuvaInfo({5, 5},
37 SkYUVAInfo::PlaneConfig::kY_U_V,
38 SkYUVAInfo::Subsampling::k420,
39 kRec601_Limited_SkYUVColorSpace);
40 SkYUVAPixmapInfo yuvaPixmapInfo(yuvaInfo,
41 SkYUVAPixmapInfo::DataType::kUnorm8,
42 /*rowBytes[]*/ nullptr);
43 SkYUVAPixmaps yuvaPixmaps;
44 const uint32_t genID = 12345678;
45
46 SkCachedData* data = SkYUVPlanesCache::FindAndRef(genID, &yuvaPixmaps, &cache);
47 REPORTER_ASSERT(reporter, !data);
48
49 size_t size = yuvaPixmapInfo.computeTotalBytes();
50 data = cache.newCachedData(size);
51 memset(data->writable_data(), 0xff, size);
52
53 SkPixmap pmaps[SkYUVAInfo::kMaxPlanes];
54 yuvaPixmapInfo.initPixmapsFromSingleAllocation(data->writable_data(), pmaps);
55 yuvaPixmaps = SkYUVAPixmaps::FromExternalPixmaps(yuvaInfo, pmaps);
56
57 SkYUVPlanesCache::Add(genID, data, yuvaPixmaps, &cache);
58 check_data(reporter, data, 2, kInCache, kLocked);
59
60 data->unref();
61 check_data(reporter, data, 1, kInCache, kUnlocked);
62
63 SkYUVAPixmaps yuvaPixmapsRead;
64 data = SkYUVPlanesCache::FindAndRef(genID, &yuvaPixmapsRead, &cache);
65
66 REPORTER_ASSERT(reporter, data);
67 REPORTER_ASSERT(reporter, data->size() == size);
68 REPORTER_ASSERT(reporter, yuvaPixmapsRead.yuvaInfo() == yuvaPixmaps.yuvaInfo());
69
70 for (int i = 0; i < yuvaPixmaps.numPlanes(); ++i) {
71 REPORTER_ASSERT(reporter, yuvaPixmaps.plane(i).info() == yuvaPixmapsRead.plane(i).info());
72 REPORTER_ASSERT(reporter, yuvaPixmaps.plane(i).addr() == yuvaPixmapsRead.plane(i).addr());
73 REPORTER_ASSERT(reporter, yuvaPixmaps.plane(i).rowBytes() ==
74 yuvaPixmapsRead.plane(i).rowBytes());
75 }
76
77 check_data(reporter, data, 2, kInCache, kLocked);
78
79 cache.purgeAll();
80 check_data(reporter, data, 1, kNotInCache, kLocked);
81 data->unref();
82 }
83