sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 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 | |
Brian Salomon | 9f8ee0d | 2020-10-19 13:31:49 -0400 | [diff] [blame] | 8 | #include "include/core/SkYUVAInfo.h" |
| 9 | #include "include/core/SkYUVAPixmaps.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "src/core/SkCachedData.h" |
| 11 | #include "src/core/SkResourceCache.h" |
| 12 | #include "src/core/SkYUVPlanesCache.h" |
| 13 | #include "tests/Test.h" |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 14 | |
| 15 | enum LockedState { |
| 16 | kUnlocked, |
| 17 | kLocked, |
| 18 | }; |
| 19 | |
| 20 | enum CachedState { |
| 21 | kNotInCache, |
| 22 | kInCache, |
| 23 | }; |
| 24 | |
| 25 | static 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)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 29 | bool isLocked = (data->data() != nullptr); |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 30 | REPORTER_ASSERT(reporter, isLocked == (lockedState == kLocked)); |
| 31 | } |
| 32 | |
| 33 | DEF_TEST(YUVPlanesCache, reporter) { |
| 34 | SkResourceCache cache(1024); |
| 35 | |
Brian Salomon | 9f8ee0d | 2020-10-19 13:31:49 -0400 | [diff] [blame] | 36 | 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; |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 43 | const uint32_t genID = 12345678; |
| 44 | |
Brian Salomon | 9f8ee0d | 2020-10-19 13:31:49 -0400 | [diff] [blame] | 45 | SkCachedData* data = SkYUVPlanesCache::FindAndRef(genID, &yuvaPixmaps, &cache); |
| 46 | REPORTER_ASSERT(reporter, !data); |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 47 | |
Brian Salomon | 9f8ee0d | 2020-10-19 13:31:49 -0400 | [diff] [blame] | 48 | size_t size = yuvaPixmapInfo.computeTotalBytes(); |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 49 | data = cache.newCachedData(size); |
| 50 | memset(data->writable_data(), 0xff, size); |
| 51 | |
Brian Salomon | 9f8ee0d | 2020-10-19 13:31:49 -0400 | [diff] [blame] | 52 | 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); |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 57 | check_data(reporter, data, 2, kInCache, kLocked); |
| 58 | |
| 59 | data->unref(); |
| 60 | check_data(reporter, data, 1, kInCache, kUnlocked); |
| 61 | |
Brian Salomon | 9f8ee0d | 2020-10-19 13:31:49 -0400 | [diff] [blame] | 62 | SkYUVAPixmaps yuvaPixmapsRead; |
| 63 | data = SkYUVPlanesCache::FindAndRef(genID, &yuvaPixmapsRead, &cache); |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 64 | |
| 65 | REPORTER_ASSERT(reporter, data); |
| 66 | REPORTER_ASSERT(reporter, data->size() == size); |
Brian Salomon | 9f8ee0d | 2020-10-19 13:31:49 -0400 | [diff] [blame] | 67 | REPORTER_ASSERT(reporter, yuvaPixmapsRead.yuvaInfo() == yuvaPixmaps.yuvaInfo()); |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 68 | |
Brian Salomon | 9f8ee0d | 2020-10-19 13:31:49 -0400 | [diff] [blame] | 69 | 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()); |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 74 | } |
Jim Van Verth | 8f11e43 | 2018-10-18 14:36:59 -0400 | [diff] [blame] | 75 | |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 76 | check_data(reporter, data, 2, kInCache, kLocked); |
| 77 | |
| 78 | cache.purgeAll(); |
| 79 | check_data(reporter, data, 1, kNotInCache, kLocked); |
| 80 | data->unref(); |
| 81 | } |