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 "SkThread.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() const { this->internalRef(false); } | |
27 void unref() const { this->internalUnref(false); } | |
28 | |
29 int testing_only_getRefCnt() const { return fRefCnt >> 1; } | |
30 bool testing_only_isInCache() const { return fRefCnt & 1; } | |
31 | |
32 protected: | |
33 // called when fData changes. could be NULL. | |
34 virtual void onDataChange(void* oldData, void* newData) {} | |
35 | |
36 private: | |
37 SkMutex fMutex; // could use a pool of these... | |
38 | |
39 enum StorageType { | |
40 kDiscardableMemory_StorageType, | |
41 kMalloc_StorageType | |
42 }; | |
43 | |
44 union { | |
45 SkDiscardableMemory* fDM; | |
46 void* fMalloc; | |
47 } fStorage; | |
48 void* fData; | |
49 size_t fSize; | |
50 int32_t fRefCnt; // low-bit means we're owned by the cach e | |
mtklein
2014/09/26 19:06:33
Now that you've got a mutex, why don't we go back
reed1
2014/09/26 21:33:51
Done.
| |
51 StorageType fStorageType; | |
52 | |
53 void internalRef(bool fromCache) const; | |
54 void internalUnref(bool fromCache) const; | |
55 bool doInternalUnref(bool fromCache) const; | |
56 | |
57 SkMutex& get_mutex() { return fMutex; } | |
58 void in_mutex_lock(); | |
59 void in_mutex_unlock(); | |
60 void in_mutex_prepareToDelete(); | |
61 | |
62 void assert_in_mutex() { | |
63 fMutex.assertHeld(); | |
64 } | |
65 | |
66 // called whenever our fData might change (lock or unlock) | |
67 void setData(void* newData) { | |
68 if (newData != fData) { | |
69 // notify our subclasses of the change | |
70 this->onDataChange(fData, newData); | |
71 fData = newData; | |
72 } | |
73 } | |
74 | |
75 | |
76 public: | |
77 /* | |
78 * Attaching a data to to a SkResourceCache (only one at a time) enables th e data to be | |
79 * unlocked when the cache is the only owner, thus freeing it to be purged (assuming the | |
80 * data is backed by a SkDiscardableMemory). | |
81 * | |
82 * When attached, it also automatically attempts to "lock" the data when th e first client | |
83 * ref's the data (typically from a find(key, visitor) call). | |
84 * | |
85 * Thus the data will always be "locked" when a non-cache has a ref on it ( whether or not | |
86 * the lock succeeded to recover the memory -- check data() to see if it is NULL). | |
87 */ | |
88 | |
89 /* | |
90 * Call when adding this instance to a SkResourceCache::Rec subclass | |
91 * (typically in the Rec's constructor). | |
92 */ | |
93 void attachToCacheAndRef() const { this->internalRef(true); } | |
94 | |
95 /* | |
96 * Call when removing this instance from a SkResourceCache::Rec subclass | |
97 * (typically in the Rec's destructor). | |
98 */ | |
99 void detachFromCacheAndUnref() const { this->internalUnref(true); } | |
100 }; | |
101 | |
102 #endif | |
OLD | NEW |