2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkBitmapCache.h"
9 #include "SkResourceCache.h"
10 #include "SkYUVPlanesCache.h"
12 #define CHECK_LOCAL(localCache, localName, globalName, ...) \
13 ((localCache) ? localCache->localName(__VA_ARGS__) : SkResourceCache::globalName(__VA_ARGS__))
16 static unsigned gYUVPlanesKeyNamespaceLabel;
19 SkYUVPlanesCache::Info fInfo;
23 struct YUVPlanesKey : public SkResourceCache::Key {
24 YUVPlanesKey(uint32_t genID)
27 this->init(&gYUVPlanesKeyNamespaceLabel, SkMakeResourceCacheSharedIDForBitmap(genID),
34 struct YUVPlanesRec : public SkResourceCache::Rec {
35 YUVPlanesRec(YUVPlanesKey key, SkCachedData* data, SkYUVPlanesCache::Info* info)
40 fValue.fData->attachToCacheAndRef();
43 fValue.fData->detachFromCacheAndUnref();
49 const Key& getKey() const SK_OVERRIDE { return fKey; }
50 size_t bytesUsed() const SK_OVERRIDE { return sizeof(*this) + fValue.fData->size(); }
52 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextData) {
53 const YUVPlanesRec& rec = static_cast<const YUVPlanesRec&>(baseRec);
54 YUVValue* result = static_cast<YUVValue*>(contextData);
56 SkCachedData* tmpData = rec.fValue.fData;
58 if (NULL == tmpData->data()) {
62 result->fData = tmpData;
63 result->fInfo = rec.fValue.fInfo;
69 SkCachedData* SkYUVPlanesCache::FindAndRef(uint32_t genID, Info* info,
70 SkResourceCache* localCache) {
72 YUVPlanesKey key(genID);
73 if (!CHECK_LOCAL(localCache, find, Find, key, YUVPlanesRec::Visitor, &result)) {
81 void SkYUVPlanesCache::Add(uint32_t genID, SkCachedData* data, Info* info,
82 SkResourceCache* localCache) {
83 YUVPlanesKey key(genID);
84 return CHECK_LOCAL(localCache, add, Add, SkNEW_ARGS(YUVPlanesRec, (key, data, info)));