blob: 95794f8f18b64e12358eaef1a5aa91964a4249cd [file] [log] [blame]
sugoi692135f2015-01-19 10:10:27 -08001/*
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
Brian Salomon9f8ee0d2020-10-19 13:31:49 -04008#include "include/core/SkYUVAInfo.h"
9#include "include/core/SkYUVAPixmaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "src/core/SkCachedData.h"
11#include "src/core/SkResourceCache.h"
12#include "src/core/SkYUVPlanesCache.h"
13#include "tests/Test.h"
sugoi692135f2015-01-19 10:10:27 -080014
15enum LockedState {
16 kUnlocked,
17 kLocked,
18};
19
20enum CachedState {
21 kNotInCache,
22 kInCache,
23};
24
25static 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));
halcanary96fcdcc2015-08-27 07:41:13 -070029 bool isLocked = (data->data() != nullptr);
sugoi692135f2015-01-19 10:10:27 -080030 REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked));
31}
32
33DEF_TEST(YUVPlanesCache, reporter) {
34 SkResourceCache cache(1024);
35
Brian Salomon9f8ee0d2020-10-19 13:31:49 -040036 SkYUVAInfo yuvaInfo({5, 5},
37 SkYUVAInfo::PlanarConfig::kY_U_V_420,
38 kRec601_Limited_SkYUVColorSpace);
39 SkYUVAPixmapInfo yuvaPixmapInfo(yuvaInfo,
40 SkYUVAPixmapInfo::DataType::kUnorm8,
41 /*rowBytes[]*/ nullptr);
42 SkYUVAPixmaps yuvaPixmaps;
sugoi692135f2015-01-19 10:10:27 -080043 const uint32_t genID = 12345678;
44
Brian Salomon9f8ee0d2020-10-19 13:31:49 -040045 SkCachedData* data = SkYUVPlanesCache::FindAndRef(genID, &yuvaPixmaps, &cache);
46 REPORTER_ASSERT(reporter, !data);
sugoi692135f2015-01-19 10:10:27 -080047
Brian Salomon9f8ee0d2020-10-19 13:31:49 -040048 size_t size = yuvaPixmapInfo.computeTotalBytes();
sugoi692135f2015-01-19 10:10:27 -080049 data = cache.newCachedData(size);
50 memset(data->writable_data(), 0xff, size);
51
Brian Salomon9f8ee0d2020-10-19 13:31:49 -040052 SkPixmap pmaps[SkYUVAInfo::kMaxPlanes];
53 yuvaPixmapInfo.initPixmapsFromSingleAllocation(data->writable_data(), pmaps);
54 yuvaPixmaps = SkYUVAPixmaps::FromExternalPixmaps(yuvaInfo, pmaps);
55
56 SkYUVPlanesCache::Add(genID, data, yuvaPixmaps, &cache);
sugoi692135f2015-01-19 10:10:27 -080057 check_data(reporter, data, 2, kInCache, kLocked);
58
59 data->unref();
60 check_data(reporter, data, 1, kInCache, kUnlocked);
61
Brian Salomon9f8ee0d2020-10-19 13:31:49 -040062 SkYUVAPixmaps yuvaPixmapsRead;
63 data = SkYUVPlanesCache::FindAndRef(genID, &yuvaPixmapsRead, &cache);
sugoi692135f2015-01-19 10:10:27 -080064
65 REPORTER_ASSERT(reporter, data);
66 REPORTER_ASSERT(reporter, data->size() == size);
Brian Salomon9f8ee0d2020-10-19 13:31:49 -040067 REPORTER_ASSERT(reporter, yuvaPixmapsRead.yuvaInfo() == yuvaPixmaps.yuvaInfo());
Jim Van Verth8f11e432018-10-18 14:36:59 -040068
Brian Salomon9f8ee0d2020-10-19 13:31:49 -040069 for (int i = 0; i < yuvaPixmaps.numPlanes(); ++i) {
70 REPORTER_ASSERT(reporter, yuvaPixmaps.plane(i).info() == yuvaPixmapsRead.plane(i).info());
71 REPORTER_ASSERT(reporter, yuvaPixmaps.plane(i).addr() == yuvaPixmapsRead.plane(i).addr());
72 REPORTER_ASSERT(reporter, yuvaPixmaps.plane(i).rowBytes() ==
73 yuvaPixmapsRead.plane(i).rowBytes());
sugoi692135f2015-01-19 10:10:27 -080074 }
Jim Van Verth8f11e432018-10-18 14:36:59 -040075
sugoi692135f2015-01-19 10:10:27 -080076 check_data(reporter, data, 2, kInCache, kLocked);
77
78 cache.purgeAll();
79 check_data(reporter, data, 1, kNotInCache, kLocked);
80 data->unref();
81}