blob: b34cf060a5b184a0f84e5eba10b6d588a359def3 [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
8#include "SkCachedData.h"
9#include "SkYUVPlanesCache.h"
10#include "SkResourceCache.h"
11#include "Test.h"
12
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;
msarett4984c3c2016-03-10 05:44:43 -080035 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;
sugoi692135f2015-01-19 10:10:27 -080039 }
40 yuvInfo.fColorSpace = kRec601_SkYUVColorSpace;
41
42 const uint32_t genID = 12345678;
43
44 SkCachedData* data = SkYUVPlanesCache::FindAndRef(genID, &yuvInfo, &cache);
halcanary96fcdcc2015-08-27 07:41:13 -070045 REPORTER_ASSERT(reporter, nullptr == data);
sugoi692135f2015-01-19 10:10:27 -080046
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) {
msarett4984c3c2016-03-10 05:44:43 -080063 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]);
sugoi692135f2015-01-19 10:10:27 -080069 }
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}