| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 Google Inc. | 2 * Copyright 2014 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkCachedData.h" | 8 #include "SkCachedData.h" |
| 9 #include "SkDiscardableMemory.h" | 9 #include "SkDiscardableMemory.h" |
| 10 | 10 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 delete this; | 91 delete this; |
| 92 } | 92 } |
| 93 } | 93 } |
| 94 | 94 |
| 95 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 95 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 96 | 96 |
| 97 void SkCachedData::inMutexRef(bool fromCache) { | 97 void SkCachedData::inMutexRef(bool fromCache) { |
| 98 if ((1 == fRefCnt) && fInCache) { | 98 if ((1 == fRefCnt) && fInCache) { |
| 99 this->inMutexLock(); | 99 this->inMutexLock(); |
| 100 } | 100 } |
| 101 | 101 |
| 102 fRefCnt += 1; | 102 fRefCnt += 1; |
| 103 if (fromCache) { | 103 if (fromCache) { |
| 104 SkASSERT(!fInCache); | 104 SkASSERT(!fInCache); |
| 105 fInCache = true; | 105 fInCache = true; |
| 106 } | 106 } |
| 107 } | 107 } |
| 108 | 108 |
| 109 bool SkCachedData::inMutexUnref(bool fromCache) { | 109 bool SkCachedData::inMutexUnref(bool fromCache) { |
| 110 switch (--fRefCnt) { | 110 switch (--fRefCnt) { |
| 111 case 0: | 111 case 0: |
| 112 // we're going to be deleted, so we need to be unlocked (for Discard
ableMemory) | 112 // we're going to be deleted, so we need to be unlocked (for Discard
ableMemory) |
| 113 if (fIsLocked) { | 113 if (fIsLocked) { |
| 114 this->inMutexUnlock(); | 114 this->inMutexUnlock(); |
| 115 } | 115 } |
| 116 break; | 116 break; |
| 117 case 1: | 117 case 1: |
| 118 if (fInCache && !fromCache) { | 118 if (fInCache && !fromCache) { |
| 119 // If we're down to 1 owner, and that owner is the cache, this i
t is safe | 119 // If we're down to 1 owner, and that owner is the cache, this i
t is safe |
| 120 // to unlock (and mutate fData) even if the cache is in a differ
ent thread, | 120 // to unlock (and mutate fData) even if the cache is in a differ
ent thread, |
| 121 // as the cache is NOT allowed to inspect or use fData. | 121 // as the cache is NOT allowed to inspect or use fData. |
| 122 this->inMutexUnlock(); | 122 this->inMutexUnlock(); |
| 123 } | 123 } |
| 124 break; | 124 break; |
| 125 default: | 125 default: |
| 126 break; | 126 break; |
| 127 } | 127 } |
| 128 | 128 |
| 129 if (fromCache) { | 129 if (fromCache) { |
| 130 SkASSERT(fInCache); | 130 SkASSERT(fInCache); |
| 131 fInCache = false; | 131 fInCache = false; |
| 132 } | 132 } |
| 133 | 133 |
| 134 // return true when we need to be deleted | 134 // return true when we need to be deleted |
| 135 return 0 == fRefCnt; | 135 return 0 == fRefCnt; |
| 136 } | 136 } |
| 137 | 137 |
| 138 void SkCachedData::inMutexLock() { | 138 void SkCachedData::inMutexLock() { |
| 139 fMutex.assertHeld(); | 139 fMutex.assertHeld(); |
| 140 | 140 |
| 141 SkASSERT(!fIsLocked); | 141 SkASSERT(!fIsLocked); |
| 142 fIsLocked = true; | 142 fIsLocked = true; |
| 143 | 143 |
| 144 switch (fStorageType) { | 144 switch (fStorageType) { |
| 145 case kMalloc_StorageType: | 145 case kMalloc_StorageType: |
| 146 this->setData(fStorage.fMalloc); | 146 this->setData(fStorage.fMalloc); |
| 147 break; | 147 break; |
| 148 case kDiscardableMemory_StorageType: | 148 case kDiscardableMemory_StorageType: |
| 149 if (fStorage.fDM->lock()) { | 149 if (fStorage.fDM->lock()) { |
| 150 void* ptr = fStorage.fDM->data(); | 150 void* ptr = fStorage.fDM->data(); |
| 151 SkASSERT(ptr); | 151 SkASSERT(ptr); |
| 152 this->setData(ptr); | 152 this->setData(ptr); |
| 153 } else { | 153 } else { |
| 154 this->setData(nullptr); // signal failure to lock, contents ar
e gone | 154 this->setData(nullptr); // signal failure to lock, contents ar
e gone |
| 155 } | 155 } |
| 156 break; | 156 break; |
| 157 } | 157 } |
| 158 } | 158 } |
| 159 | 159 |
| 160 void SkCachedData::inMutexUnlock() { | 160 void SkCachedData::inMutexUnlock() { |
| 161 fMutex.assertHeld(); | 161 fMutex.assertHeld(); |
| 162 | 162 |
| 163 SkASSERT(fIsLocked); | 163 SkASSERT(fIsLocked); |
| 164 fIsLocked = false; | 164 fIsLocked = false; |
| 165 | 165 |
| 166 switch (fStorageType) { | 166 switch (fStorageType) { |
| 167 case kMalloc_StorageType: | 167 case kMalloc_StorageType: |
| 168 // nothing to do/check | 168 // nothing to do/check |
| 169 break; | 169 break; |
| 170 case kDiscardableMemory_StorageType: | 170 case kDiscardableMemory_StorageType: |
| 171 if (fData) { // did the previous lock succeed? | 171 if (fData) { // did the previous lock succeed? |
| 172 fStorage.fDM->unlock(); | 172 fStorage.fDM->unlock(); |
| 173 } | 173 } |
| 174 break; | 174 break; |
| 175 } | 175 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 189 case kDiscardableMemory_StorageType: | 189 case kDiscardableMemory_StorageType: |
| 190 // fData can be null or the actual value, depending if DM's lock
succeeded | 190 // fData can be null or the actual value, depending if DM's lock
succeeded |
| 191 break; | 191 break; |
| 192 } | 192 } |
| 193 } else { | 193 } else { |
| 194 SkASSERT((fInCache && 1 == fRefCnt) || (0 == fRefCnt)); | 194 SkASSERT((fInCache && 1 == fRefCnt) || (0 == fRefCnt)); |
| 195 SkASSERT(nullptr == fData); | 195 SkASSERT(nullptr == fData); |
| 196 } | 196 } |
| 197 } | 197 } |
| 198 #endif | 198 #endif |
| OLD | NEW |