Flutter Engine
The Flutter Engine
SkYUVPlanesCache.cpp
Go to the documentation of this file.
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
9
14
15#include <cstddef>
16
18
19#define CHECK_LOCAL(localCache, localName, globalName, ...) \
20 ((localCache) ? localCache->localName(__VA_ARGS__) : SkResourceCache::globalName(__VA_ARGS__))
21
22namespace {
23static unsigned gYUVPlanesKeyNamespaceLabel;
24
25struct YUVValue {
26 SkYUVAPixmaps fPixmaps;
27 SkCachedData* fData;
28};
29
30struct YUVPlanesKey : public SkResourceCache::Key {
31 YUVPlanesKey(uint32_t genID)
32 : fGenID(genID)
33 {
34 this->init(&gYUVPlanesKeyNamespaceLabel, SkMakeResourceCacheSharedIDForBitmap(genID),
35 sizeof(genID));
36 }
37
38 uint32_t fGenID;
39};
40
41struct YUVPlanesRec : public SkResourceCache::Rec {
42 YUVPlanesRec(YUVPlanesKey key, SkCachedData* data, const SkYUVAPixmaps& pixmaps)
43 : fKey(key)
44 {
45 fValue.fData = data;
46 fValue.fPixmaps = pixmaps;
47 fValue.fData->attachToCacheAndRef();
48 }
49 ~YUVPlanesRec() override {
50 fValue.fData->detachFromCacheAndUnref();
51 }
52
53 YUVPlanesKey fKey;
54 YUVValue fValue;
55
56 const Key& getKey() const override { return fKey; }
57 size_t bytesUsed() const override { return sizeof(*this) + fValue.fData->size(); }
58 const char* getCategory() const override { return "yuv-planes"; }
59 SkDiscardableMemory* diagnostic_only_getDiscardable() const override {
60 return fValue.fData->diagnostic_only_getDiscardable();
61 }
62
63 static bool Visitor(const SkResourceCache::Rec& baseRec, void* contextData) {
64 const YUVPlanesRec& rec = static_cast<const YUVPlanesRec&>(baseRec);
65 YUVValue* result = static_cast<YUVValue*>(contextData);
66
67 SkCachedData* tmpData = rec.fValue.fData;
68 tmpData->ref();
69 if (nullptr == tmpData->data()) {
70 tmpData->unref();
71 return false;
72 }
73 result->fData = tmpData;
74 result->fPixmaps = rec.fValue.fPixmaps;
75 return true;
76 }
77};
78} // namespace
79
81 SkYUVAPixmaps* pixmaps,
82 SkResourceCache* localCache) {
83 YUVValue result;
84 YUVPlanesKey key(genID);
85 if (!CHECK_LOCAL(localCache, find, Find, key, YUVPlanesRec::Visitor, &result)) {
86 return nullptr;
87 }
88
89 *pixmaps = result.fPixmaps;
90 return result.fData;
91}
92
93void SkYUVPlanesCache::Add(uint32_t genID, SkCachedData* data, const SkYUVAPixmaps& pixmaps,
94 SkResourceCache* localCache) {
95 YUVPlanesKey key(genID);
96 return CHECK_LOCAL(localCache, add, Add, new YUVPlanesRec(key, data, pixmaps));
97}
TArray< uint32_t > Key
uint64_t SkMakeResourceCacheSharedIDForBitmap(uint32_t bitmapGenID)
#define CHECK_LOCAL(localCache, localName, globalName,...)
int find(T *array, int N, T item)
void ref() const
Definition: SkCachedData.h:30
void unref() const
Definition: SkCachedData.h:31
const void * data() const
Definition: SkCachedData.h:26
virtual void * data()=0
static void Add(uint32_t genID, SkCachedData *data, const SkYUVAPixmaps &pixmaps, SkResourceCache *localCache=nullptr)
static SkCachedData * FindAndRef(uint32_t genID, SkYUVAPixmaps *pixmaps, SkResourceCache *localCache=nullptr)
GAsyncResult * result
static bool init()
Visitor(Ts...) -> Visitor< Ts... >
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63