blob: c8fb9dc2f4d10dab2e54b48575764319a92b12a8 [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},
Brian Salomone4387382020-11-11 16:34:19 -050037 SkYUVAInfo::PlaneConfig::kY_U_V,
38 SkYUVAInfo::Subsampling::k420,
Brian Salomon9f8ee0d2020-10-19 13:31:49 -040039 kRec601_Limited_SkYUVColorSpace);
40 SkYUVAPixmapInfo yuvaPixmapInfo(yuvaInfo,
41 SkYUVAPixmapInfo::DataType::kUnorm8,
42 /*rowBytes[]*/ nullptr);
43 SkYUVAPixmaps yuvaPixmaps;
sugoi692135f2015-01-19 10:10:27 -080044 const uint32_t genID = 12345678;
45
Brian Salomon9f8ee0d2020-10-19 13:31:49 -040046 SkCachedData* data = SkYUVPlanesCache::FindAndRef(genID, &yuvaPixmaps, &cache);
47 REPORTER_ASSERT(reporter, !data);
sugoi692135f2015-01-19 10:10:27 -080048
Brian Salomon9f8ee0d2020-10-19 13:31:49 -040049 size_t size = yuvaPixmapInfo.computeTotalBytes();
sugoi692135f2015-01-19 10:10:27 -080050 data = cache.newCachedData(size);
51 memset(data->writable_data(), 0xff, size);
52
Brian Salomon9f8ee0d2020-10-19 13:31:49 -040053 SkPixmap pmaps[SkYUVAInfo::kMaxPlanes];
54 yuvaPixmapInfo.initPixmapsFromSingleAllocation(data->writable_data(), pmaps);
55 yuvaPixmaps = SkYUVAPixmaps::FromExternalPixmaps(yuvaInfo, pmaps);
56
57 SkYUVPlanesCache::Add(genID, data, yuvaPixmaps, &cache);
sugoi692135f2015-01-19 10:10:27 -080058 check_data(reporter, data, 2, kInCache, kLocked);
59
60 data->unref();
61 check_data(reporter, data, 1, kInCache, kUnlocked);
62
Brian Salomon9f8ee0d2020-10-19 13:31:49 -040063 SkYUVAPixmaps yuvaPixmapsRead;
64 data = SkYUVPlanesCache::FindAndRef(genID, &yuvaPixmapsRead, &cache);
sugoi692135f2015-01-19 10:10:27 -080065
66 REPORTER_ASSERT(reporter, data);
67 REPORTER_ASSERT(reporter, data->size() == size);
Brian Salomon9f8ee0d2020-10-19 13:31:49 -040068 REPORTER_ASSERT(reporter, yuvaPixmapsRead.yuvaInfo() == yuvaPixmaps.yuvaInfo());
Jim Van Verth8f11e432018-10-18 14:36:59 -040069
Brian Salomon9f8ee0d2020-10-19 13:31:49 -040070 for (int i = 0; i < yuvaPixmaps.numPlanes(); ++i) {
71 REPORTER_ASSERT(reporter, yuvaPixmaps.plane(i).info() == yuvaPixmapsRead.plane(i).info());
72 REPORTER_ASSERT(reporter, yuvaPixmaps.plane(i).addr() == yuvaPixmapsRead.plane(i).addr());
73 REPORTER_ASSERT(reporter, yuvaPixmaps.plane(i).rowBytes() ==
74 yuvaPixmapsRead.plane(i).rowBytes());
sugoi692135f2015-01-19 10:10:27 -080075 }
Jim Van Verth8f11e432018-10-18 14:36:59 -040076
sugoi692135f2015-01-19 10:10:27 -080077 check_data(reporter, data, 2, kInCache, kLocked);
78
79 cache.purgeAll();
80 check_data(reporter, data, 1, kNotInCache, kLocked);
81 data->unref();
82}