OLD | NEW |
(Empty) | |
| 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 |
| 8 #include "SkYUVPlanesCache.h" |
| 9 #include "SkResourceCache.h" |
| 10 |
| 11 #define CHECK_LOCAL(localCache, localName, globalName, ...) \ |
| 12 ((localCache) ? localCache->localName(__VA_ARGS__) : SkResourceCache::global
Name(__VA_ARGS__)) |
| 13 |
| 14 namespace { |
| 15 static unsigned gYUVPlanesKeyNamespaceLabel; |
| 16 |
| 17 struct YUVValue { |
| 18 SkYUVPlanesCache::Info fInfo; |
| 19 SkCachedData* fData; |
| 20 }; |
| 21 |
| 22 struct YUVPlanesKey : public SkResourceCache::Key { |
| 23 YUVPlanesKey(uint32_t genID) |
| 24 : fGenID(genID) |
| 25 { |
| 26 this->init(&gYUVPlanesKeyNamespaceLabel, sizeof(genID)); |
| 27 } |
| 28 |
| 29 uint32_t fGenID; |
| 30 }; |
| 31 |
| 32 struct YUVPlanesRec : public SkResourceCache::Rec { |
| 33 YUVPlanesRec(YUVPlanesKey key, SkCachedData* data, SkYUVPlanesCache::Info* i
nfo) |
| 34 : fKey(key) |
| 35 { |
| 36 fValue.fData = data; |
| 37 fValue.fInfo = *info; |
| 38 fValue.fData->attachToCacheAndRef(); |
| 39 } |
| 40 ~YUVPlanesRec() { |
| 41 fValue.fData->detachFromCacheAndUnref(); |
| 42 } |
| 43 |
| 44 YUVPlanesKey fKey; |
| 45 YUVValue fValue; |
| 46 |
| 47 const Key& getKey() const SK_OVERRIDE { return fKey; } |
| 48 size_t bytesUsed() const SK_OVERRIDE { return sizeof(*this) + fValue.fData->
size(); } |
| 49 |
| 50 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextData)
{ |
| 51 const YUVPlanesRec& rec = static_cast<const YUVPlanesRec&>(baseRec); |
| 52 YUVValue* result = static_cast<YUVValue*>(contextData); |
| 53 |
| 54 SkCachedData* tmpData = rec.fValue.fData; |
| 55 tmpData->ref(); |
| 56 if (NULL == tmpData->data()) { |
| 57 tmpData->unref(); |
| 58 return false; |
| 59 } |
| 60 result->fData = tmpData; |
| 61 result->fInfo = rec.fValue.fInfo; |
| 62 return true; |
| 63 } |
| 64 }; |
| 65 } // namespace |
| 66 |
| 67 SkCachedData* SkYUVPlanesCache::FindAndRef(uint32_t genID, Info* info, |
| 68 SkResourceCache* localCache) { |
| 69 YUVValue result; |
| 70 YUVPlanesKey key(genID); |
| 71 if (!CHECK_LOCAL(localCache, find, Find, key, YUVPlanesRec::Visitor, &result
)) { |
| 72 return NULL; |
| 73 } |
| 74 |
| 75 *info = result.fInfo; |
| 76 return result.fData; |
| 77 } |
| 78 |
| 79 void SkYUVPlanesCache::Add(uint32_t genID, SkCachedData* data, Info* info, |
| 80 SkResourceCache* localCache) { |
| 81 YUVPlanesKey key(genID); |
| 82 return CHECK_LOCAL(localCache, add, Add, SkNEW_ARGS(YUVPlanesRec, (key, data
, info))); |
| 83 } |
OLD | NEW |