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