Debian Codegraph @

src/core/SkCachedData.cpp

raw ◀ ▶
1 Debian patch touch this file
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 #include "src/core/SkCachedData.h"
8
9 #include "include/private/base/SkMalloc.h"
10 #include "include/private/chromium/SkDiscardableMemory.h"
11
12 SkCachedData::SkCachedData(void* data, size_t size)
13 : fData(data)
14 , fSize(size)
15 , fRefCnt(1)
16 , fStorageType(kMalloc_StorageType)
17 , fInCache(false)
18 , fIsLocked(true)
19 {
20 fStorage.fMalloc = data;
21 }
22
23 SkCachedData::SkCachedData(size_t size, SkDiscardableMemory* dm)
24 : fData(dm->data())
25 , fSize(size)
26 , fRefCnt(1)
27 , fStorageType(kDiscardableMemory_StorageType)
28 , fInCache(false)
29 , fIsLocked(true)
30 {
31 fStorage.fDM = dm;
32 }
33
34 SkCachedData::~SkCachedData() {
35 switch (fStorageType) {
36 case kMalloc_StorageType:
37 sk_free(fStorage.fMalloc);
38 break;
39 case kDiscardableMemory_StorageType:
40 delete fStorage.fDM;
41 break;
42 }
43 }
44
45 class SkCachedData::AutoMutexWritable {
46 public:
47 AutoMutexWritable(const SkCachedData* cd) : fCD(const_cast<SkCachedData*>(cd)) {
48 fCD->fMutex.acquire();
49 fCD->validate();
50 }
51 ~AutoMutexWritable() {
52 fCD->validate();
53 fCD->fMutex.release();
54 }
55
56 SkCachedData* get() { return fCD; }
57 SkCachedData* operator->() { return fCD; }
58
59 private:
60 SkCachedData* fCD;
61 };
62
63 void SkCachedData::internalRef(bool fromCache) const {
64 AutoMutexWritable(this)->inMutexRef(fromCache);
65 }
66
67 void SkCachedData::internalUnref(bool fromCache) const {
•68 bool shouldDelete = false;
69 {
70 AutoMutexWritable amw(this);
71 shouldDelete = amw->inMutexUnref(fromCache);
72 }
73 if (shouldDelete) {
74 // can't delete inside doInternalUnref, since it is locking a mutex (which we own)
75 delete this;
76 }
77 }
78
79 ///////////////////////////////////////////////////////////////////////////////////////////////////
80
81 void SkCachedData::inMutexRef(bool fromCache) {
82 if ((1 == fRefCnt) && fInCache) {
83 this->inMutexLock();
84 }
85
86 fRefCnt += 1;
87 if (fromCache) {
88 SkASSERT(!fInCache);
89 fInCache = true;
90 }
91 }
92
93 bool SkCachedData::inMutexUnref(bool fromCache) {
94 switch (--fRefCnt) {
95 case 0:
96 // we're going to be deleted, so we need to be unlocked (for DiscardableMemory)
97 if (fIsLocked) {
98 this->inMutexUnlock();
99 }
100 break;
101 case 1:
102 if (fInCache && !fromCache) {
103 // If we're down to 1 owner, and that owner is the cache, this it is safe
104 // to unlock (and mutate fData) even if the cache is in a different thread,
105 // as the cache is NOT allowed to inspect or use fData.
106 this->inMutexUnlock();
107 }
108 break;
109 default:
110 break;
111 }
112
113 if (fromCache) {
114 SkASSERT(fInCache);
115 fInCache = false;
116 }
117
118 // return true when we need to be deleted
119 return 0 == fRefCnt;
120 }
121
122 void SkCachedData::inMutexLock() {
123 fMutex.assertHeld();
124
125 SkASSERT(!fIsLocked);
126 fIsLocked = true;
127
128 switch (fStorageType) {
129 case kMalloc_StorageType:
130 this->setData(fStorage.fMalloc);
131 break;
132 case kDiscardableMemory_StorageType:
133 if (fStorage.fDM->lock()) {
134 void* ptr = fStorage.fDM->data();
135 SkASSERT(ptr);
136 this->setData(ptr);
137 } else {
138 this->setData(nullptr); // signal failure to lock, contents are gone
139 }
140 break;
141 }
142 }
143
144 void SkCachedData::inMutexUnlock() {
145 fMutex.assertHeld();
146
147 SkASSERT(fIsLocked);
148 fIsLocked = false;
149
150 switch (fStorageType) {
151 case kMalloc_StorageType:
152 // nothing to do/check
153 break;
154 case kDiscardableMemory_StorageType:
155 if (fData) { // did the previous lock succeed?
156 fStorage.fDM->unlock();
157 }
158 break;
159 }
160 this->setData(nullptr); // signal that we're in an unlocked state
161 }
162
163 ///////////////////////////////////////////////////////////////////////////////////////////////////
164
165 #ifdef SK_DEBUG
166 void SkCachedData::validate() const {
167 if (fIsLocked) {
168 SkASSERT((fInCache && fRefCnt > 1) || !fInCache);
169 switch (fStorageType) {
170 case kMalloc_StorageType:
171 SkASSERT(fData == fStorage.fMalloc);
172 break;
173 case kDiscardableMemory_StorageType:
174 // fData can be null or the actual value, depending if DM's lock succeeded
175 break;
176 }
177 } else {
178 SkASSERT((fInCache && 1 == fRefCnt) || (0 == fRefCnt));
179 SkASSERT(nullptr == fData);
180 }
181 }
182 #endif