sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 8 | #include "SkBitmapCache.h" |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 9 | #include "SkResourceCache.h" |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 10 | #include "SkYUVPlanesCache.h" |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 11 | |
| 12 | #define CHECK_LOCAL(localCache, localName, globalName, ...) \ |
| 13 | ((localCache) ? localCache->localName(__VA_ARGS__) : SkResourceCache::globalName(__VA_ARGS__)) |
| 14 | |
| 15 | namespace { |
| 16 | static unsigned gYUVPlanesKeyNamespaceLabel; |
| 17 | |
| 18 | struct YUVValue { |
| 19 | SkYUVPlanesCache::Info fInfo; |
| 20 | SkCachedData* fData; |
| 21 | }; |
| 22 | |
| 23 | struct YUVPlanesKey : public SkResourceCache::Key { |
| 24 | YUVPlanesKey(uint32_t genID) |
| 25 | : fGenID(genID) |
| 26 | { |
reed | 7eeba25 | 2015-02-24 13:54:23 -0800 | [diff] [blame] | 27 | this->init(&gYUVPlanesKeyNamespaceLabel, SkMakeResourceCacheSharedIDForBitmap(genID), |
| 28 | sizeof(genID)); |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | uint32_t fGenID; |
| 32 | }; |
| 33 | |
| 34 | struct YUVPlanesRec : public SkResourceCache::Rec { |
| 35 | YUVPlanesRec(YUVPlanesKey key, SkCachedData* data, SkYUVPlanesCache::Info* info) |
| 36 | : fKey(key) |
| 37 | { |
| 38 | fValue.fData = data; |
| 39 | fValue.fInfo = *info; |
| 40 | fValue.fData->attachToCacheAndRef(); |
| 41 | } |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 42 | ~YUVPlanesRec() override { |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 43 | fValue.fData->detachFromCacheAndUnref(); |
| 44 | } |
| 45 | |
| 46 | YUVPlanesKey fKey; |
| 47 | YUVValue fValue; |
| 48 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 49 | const Key& getKey() const override { return fKey; } |
| 50 | size_t bytesUsed() const override { return sizeof(*this) + fValue.fData->size(); } |
reed | 216b643 | 2015-08-19 12:25:40 -0700 | [diff] [blame] | 51 | const char* getCategory() const override { return "yuv-planes"; } |
| 52 | SkDiscardableMemory* diagnostic_only_getDiscardable() const override { |
| 53 | return fValue.fData->diagnostic_only_getDiscardable(); |
| 54 | } |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 55 | |
| 56 | static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextData) { |
| 57 | const YUVPlanesRec& rec = static_cast<const YUVPlanesRec&>(baseRec); |
| 58 | YUVValue* result = static_cast<YUVValue*>(contextData); |
| 59 | |
| 60 | SkCachedData* tmpData = rec.fValue.fData; |
| 61 | tmpData->ref(); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 62 | if (nullptr == tmpData->data()) { |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 63 | tmpData->unref(); |
| 64 | return false; |
| 65 | } |
| 66 | result->fData = tmpData; |
| 67 | result->fInfo = rec.fValue.fInfo; |
| 68 | return true; |
| 69 | } |
| 70 | }; |
| 71 | } // namespace |
| 72 | |
| 73 | SkCachedData* SkYUVPlanesCache::FindAndRef(uint32_t genID, Info* info, |
| 74 | SkResourceCache* localCache) { |
| 75 | YUVValue result; |
| 76 | YUVPlanesKey key(genID); |
| 77 | if (!CHECK_LOCAL(localCache, find, Find, key, YUVPlanesRec::Visitor, &result)) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 78 | return nullptr; |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 79 | } |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 80 | |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 81 | *info = result.fInfo; |
| 82 | return result.fData; |
| 83 | } |
| 84 | |
| 85 | void SkYUVPlanesCache::Add(uint32_t genID, SkCachedData* data, Info* info, |
| 86 | SkResourceCache* localCache) { |
| 87 | YUVPlanesKey key(genID); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 88 | return CHECK_LOCAL(localCache, add, Add, new YUVPlanesRec(key, data, info)); |
sugoi | 692135f | 2015-01-19 10:10:27 -0800 | [diff] [blame] | 89 | } |