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 "src/core/SkYUVPlanesCache.h"
9
10 #include "include/core/SkYUVAPixmaps.h"
11 #include "src/core/SkBitmapCache.h"
12 #include "src/core/SkCachedData.h"
13 #include "src/core/SkResourceCache.h"
14
15 #define CHECK_LOCAL(localCache, localName, globalName, ...) \
16 ((localCache) ? localCache->localName(__VA_ARGS__) : SkResourceCache::globalName(__VA_ARGS__))
17
18 namespace {
19 static unsigned gYUVPlanesKeyNamespaceLabel;
20
21 struct YUVValue {
22 SkYUVAPixmaps fPixmaps;
23 SkCachedData* fData;
24 };
25
26 struct YUVPlanesKey : public SkResourceCache::Key {
YUVPlanesKey__anon13c2ae460111::YUVPlanesKey27 YUVPlanesKey(uint32_t genID)
28 : fGenID(genID)
29 {
30 this->init(&gYUVPlanesKeyNamespaceLabel, SkMakeResourceCacheSharedIDForBitmap(genID),
31 sizeof(genID));
32 }
33
34 uint32_t fGenID;
35 };
36
37 struct YUVPlanesRec : public SkResourceCache::Rec {
YUVPlanesRec__anon13c2ae460111::YUVPlanesRec38 YUVPlanesRec(YUVPlanesKey key, SkCachedData* data, const SkYUVAPixmaps& pixmaps)
39 : fKey(key)
40 {
41 fValue.fData = data;
42 fValue.fPixmaps = pixmaps;
43 fValue.fData->attachToCacheAndRef();
44 }
~YUVPlanesRec__anon13c2ae460111::YUVPlanesRec45 ~YUVPlanesRec() override {
46 fValue.fData->detachFromCacheAndUnref();
47 }
48
49 YUVPlanesKey fKey;
50 YUVValue fValue;
51
getKey__anon13c2ae460111::YUVPlanesRec52 const Key& getKey() const override { return fKey; }
bytesUsed__anon13c2ae460111::YUVPlanesRec53 size_t bytesUsed() const override { return sizeof(*this) + fValue.fData->size(); }
getCategory__anon13c2ae460111::YUVPlanesRec54 const char* getCategory() const override { return "yuv-planes"; }
diagnostic_only_getDiscardable__anon13c2ae460111::YUVPlanesRec55 SkDiscardableMemory* diagnostic_only_getDiscardable() const override {
56 return fValue.fData->diagnostic_only_getDiscardable();
57 }
58
Visitor__anon13c2ae460111::YUVPlanesRec59 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextData) {
60 const YUVPlanesRec& rec = static_cast<const YUVPlanesRec&>(baseRec);
61 YUVValue* result = static_cast<YUVValue*>(contextData);
62
63 SkCachedData* tmpData = rec.fValue.fData;
64 tmpData->ref();
65 if (nullptr == tmpData->data()) {
66 tmpData->unref();
67 return false;
68 }
69 result->fData = tmpData;
70 result->fPixmaps = rec.fValue.fPixmaps;
71 return true;
72 }
73 };
74 } // namespace
75
FindAndRef(uint32_t genID,SkYUVAPixmaps * pixmaps,SkResourceCache * localCache)76 SkCachedData* SkYUVPlanesCache::FindAndRef(uint32_t genID,
77 SkYUVAPixmaps* pixmaps,
78 SkResourceCache* localCache) {
79 YUVValue result;
80 YUVPlanesKey key(genID);
81 if (!CHECK_LOCAL(localCache, find, Find, key, YUVPlanesRec::Visitor, &result)) {
82 return nullptr;
83 }
84
85 *pixmaps = result.fPixmaps;
86 return result.fData;
87 }
88
Add(uint32_t genID,SkCachedData * data,const SkYUVAPixmaps & pixmaps,SkResourceCache * localCache)89 void SkYUVPlanesCache::Add(uint32_t genID, SkCachedData* data, const SkYUVAPixmaps& pixmaps,
90 SkResourceCache* localCache) {
91 YUVPlanesKey key(genID);
92 return CHECK_LOCAL(localCache, add, Add, new YUVPlanesRec(key, data, pixmaps));
93 }
94