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 "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));
27    bool isLocked = (data->data() != nullptr);
28    REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked));
29}
30
31DEF_TEST(YUVPlanesCache, reporter) {
32    SkResourceCache cache(1024);
33
34    SkYUVPlanesCache::Info yuvInfo;
35    for (int i = 0; i < 3; ++i) {
36        yuvInfo.fSize[i].fWidth = 20 * i;
37        yuvInfo.fSize[i].fHeight = 10 * i;
38        yuvInfo.fSizeInMemory[i] = 800 * i;
39        yuvInfo.fRowBytes[i] = 80 * i;
40    }
41    yuvInfo.fColorSpace = kRec601_SkYUVColorSpace;
42
43    const uint32_t genID = 12345678;
44
45    SkCachedData* data = SkYUVPlanesCache::FindAndRef(genID, &yuvInfo, &cache);
46    REPORTER_ASSERT(reporter, nullptr == data);
47
48    size_t size = 256;
49    data = cache.newCachedData(size);
50    memset(data->writable_data(), 0xff, size);
51
52    SkYUVPlanesCache::Add(genID, data, &yuvInfo, &cache);
53    check_data(reporter, data, 2, kInCache, kLocked);
54
55    data->unref();
56    check_data(reporter, data, 1, kInCache, kUnlocked);
57
58    SkYUVPlanesCache::Info yuvInfoRead;
59    data = SkYUVPlanesCache::FindAndRef(genID, &yuvInfoRead, &cache);
60
61    REPORTER_ASSERT(reporter, data);
62    REPORTER_ASSERT(reporter, data->size() == size);
63    for (int i = 0; i < 3; ++i) {
64        REPORTER_ASSERT(reporter, yuvInfo.fSize[i].fWidth == yuvInfoRead.fSize[i].fWidth);
65        REPORTER_ASSERT(reporter, yuvInfo.fSize[i].fHeight == yuvInfoRead.fSize[i].fHeight);
66        REPORTER_ASSERT(reporter, yuvInfo.fSizeInMemory[i] == yuvInfoRead.fSizeInMemory[i]);
67        REPORTER_ASSERT(reporter, yuvInfo.fRowBytes[i] == yuvInfoRead.fRowBytes[i]);
68    }
69    REPORTER_ASSERT(reporter, yuvInfo.fColorSpace == yuvInfoRead.fColorSpace);
70
71    check_data(reporter, data, 2, kInCache, kLocked);
72
73    cache.purgeAll();
74    check_data(reporter, data, 1, kNotInCache, kLocked);
75    data->unref();
76}
77