| Index: src/core/SkCachedData.h
|
| diff --git a/src/core/SkCachedData.h b/src/core/SkCachedData.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..cb437ed0fd59177f4250aefe618e8364d6a4ab28
|
| --- /dev/null
|
| +++ b/src/core/SkCachedData.h
|
| @@ -0,0 +1,79 @@
|
| +/*
|
| + * Copyright 2014 Google Inc.
|
| + *
|
| + * Use of this source code is governed by a BSD-style license that can be
|
| + * found in the LICENSE file.
|
| + */
|
| +
|
| +#ifndef SkCachedData_DEFINED
|
| +#define SkCachedData_DEFINED
|
| +
|
| +#include "SkTypes.h"
|
| +
|
| +class SkDiscardableMemory;
|
| +
|
| +class SkCachedData : ::SkNoncopyable {
|
| +public:
|
| + SkCachedData(void* mallocData, size_t size);
|
| + SkCachedData(size_t size, SkDiscardableMemory*);
|
| + virtual ~SkCachedData();
|
| +
|
| + size_t size() const { return fSize; }
|
| + const void* data() const { return fData; }
|
| +
|
| + void* writable_data() { return fData; }
|
| +
|
| + void ref() { this->internalRef(2); }
|
| + void unref() { this->internalUnref(2); }
|
| +
|
| + int testing_only_getRefCnt() const { return fRefCnt >> 1; }
|
| + bool testing_only_isInCache() const { return fRefCnt & 1; }
|
| +
|
| +private:
|
| + enum StorageType {
|
| + kDiscardableMemory_StorageType,
|
| + kMalloc_StorageType
|
| + };
|
| +
|
| + union {
|
| + SkDiscardableMemory* fDM;
|
| + void* fMalloc;
|
| + } fStorage;
|
| + void* fData;
|
| + size_t fSize;
|
| + int32_t fRefCnt; // low-bit means we're owned by the cache
|
| + StorageType fStorageType;
|
| +
|
| + void internalRef(int refAmount);
|
| + void internalUnref(int refAmount);
|
| +
|
| + void lock();
|
| + void unlock();
|
| +
|
| +public:
|
| + /*
|
| + * Attaching a data to to a SkResourceCache (only one at a time) enables the data to be
|
| + * unlocked when the cache is the only owner, thus freeing it to be purged (assuming the
|
| + * data is backed by a SkDiscardableMemory).
|
| + *
|
| + * When attached, it also automatically attempts to "lock" the data when the first client
|
| + * ref's the data (typically from a find(key, visitor) call).
|
| + *
|
| + * Thus the data will always be "locked" when a non-cache has a ref on it (whether or not
|
| + * the lock succeeded to recover the memory -- check data() to see if it is NULL).
|
| + */
|
| +
|
| + /*
|
| + * Call when adding this instance to a SkResourceCache::Rec subclass
|
| + * (typically in the Rec's constructor).
|
| + */
|
| + void attachToCacheAndRef() { this->internalRef(3); }
|
| +
|
| + /*
|
| + * Call when removing this instance from a SkResourceCache::Rec subclass
|
| + * (typically in the Rec's destructor).
|
| + */
|
| + void detachFromCacheAndUnref() { this->internalUnref(3); }
|
| +};
|
| +
|
| +#endif
|
|
|