OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2014 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 #ifndef SkCachedData_DEFINED |
| 9 #define SkCachedData_DEFINED |
| 10 |
| 11 #include "SkTypes.h" |
| 12 |
| 13 class SkDiscardableMemory; |
| 14 |
| 15 class SkCachedData : ::SkNoncopyable { |
| 16 public: |
| 17 SkCachedData(void* mallocData, size_t size); |
| 18 SkCachedData(size_t size, SkDiscardableMemory*); |
| 19 virtual ~SkCachedData(); |
| 20 |
| 21 size_t size() const { return fSize; } |
| 22 const void* data() const { return fData; } |
| 23 |
| 24 void* writable_data() { return fData; } |
| 25 |
| 26 void ref() { this->internalRef(2); } |
| 27 void unref() { this->internalUnref(2); } |
| 28 |
| 29 int testing_only_getRefCnt() const { return fRefCnt >> 1; } |
| 30 bool testing_only_isInCache() const { return fRefCnt & 1; } |
| 31 |
| 32 private: |
| 33 enum StorageType { |
| 34 kDiscardableMemory_StorageType, |
| 35 kMalloc_StorageType |
| 36 }; |
| 37 |
| 38 union { |
| 39 SkDiscardableMemory* fDM; |
| 40 void* fMalloc; |
| 41 } fStorage; |
| 42 void* fData; |
| 43 size_t fSize; |
| 44 int32_t fRefCnt; // low-bit means we're owned by the cach
e |
| 45 StorageType fStorageType; |
| 46 |
| 47 void internalRef(int refAmount); |
| 48 void internalUnref(int refAmount); |
| 49 |
| 50 void lock(); |
| 51 void unlock(); |
| 52 |
| 53 public: |
| 54 /* |
| 55 * Attaching a data to to a SkResourceCache (only one at a time) enables th
e data to be |
| 56 * unlocked when the cache is the only owner, thus freeing it to be purged
(assuming the |
| 57 * data is backed by a SkDiscardableMemory). |
| 58 * |
| 59 * When attached, it also automatically attempts to "lock" the data when th
e first client |
| 60 * ref's the data (typically from a find(key, visitor) call). |
| 61 * |
| 62 * Thus the data will always be "locked" when a non-cache has a ref on it (
whether or not |
| 63 * the lock succeeded to recover the memory -- check data() to see if it is
NULL). |
| 64 */ |
| 65 |
| 66 /* |
| 67 * Call when adding this instance to a SkResourceCache::Rec subclass |
| 68 * (typically in the Rec's constructor). |
| 69 */ |
| 70 void attachToCacheAndRef() { this->internalRef(3); } |
| 71 |
| 72 /* |
| 73 * Call when removing this instance from a SkResourceCache::Rec subclass |
| 74 * (typically in the Rec's destructor). |
| 75 */ |
| 76 void detachFromCacheAndUnref() { this->internalUnref(3); } |
| 77 }; |
| 78 |
| 79 #endif |
OLD | NEW |