blob: a0a41604242505dde1109075859f21ec879f2b5f [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/core/SkCachedData.h"
9#include "src/core/SkResourceCache.h"
10#include "src/core/SkYUVPlanesCache.h"
11#include "tests/Test.h"
sugoi692135f2015-01-19 10:10:27 -080012
13enum LockedState {
14 kUnlocked,
15 kLocked,
16};
17
18enum CachedState {
19 kNotInCache,
20 kInCache,
21};
22
23static 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));
halcanary96fcdcc2015-08-27 07:41:13 -070027 bool isLocked = (data->data() != nullptr);
sugoi692135f2015-01-19 10:10:27 -080028 REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked));
29}
30
31DEF_TEST(YUVPlanesCache, reporter) {
32 SkResourceCache cache(1024);
33
34 SkYUVPlanesCache::Info yuvInfo;
Jim Van Verthe24b5872018-10-29 16:26:02 -040035 for (int i = 0; i < SkYUVASizeInfo::kMaxCount; i++) {
Jim Van Verth8f11e432018-10-18 14:36:59 -040036 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;
sugoi692135f2015-01-19 10:10:27 -080044 }
45 yuvInfo.fColorSpace = kRec601_SkYUVColorSpace;
46
47 const uint32_t genID = 12345678;
48
49 SkCachedData* data = SkYUVPlanesCache::FindAndRef(genID, &yuvInfo, &cache);
halcanary96fcdcc2015-08-27 07:41:13 -070050 REPORTER_ASSERT(reporter, nullptr == data);
sugoi692135f2015-01-19 10:10:27 -080051
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);
Jim Van Verth8f11e432018-10-18 14:36:59 -040067 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]);
sugoi692135f2015-01-19 10:10:27 -080071 }
Jim Van Verth8f11e432018-10-18 14:36:59 -040072
sugoi692135f2015-01-19 10:10:27 -080073 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}