OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2013 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 #include "SkThread.h" |
| 9 #include "SkPurgeableImageCache.h" |
| 10 #include "SkPurgeableMemoryBlock.h" |
| 11 |
| 12 #ifdef SK_DEBUG |
| 13 #include "SkTSearch.h" |
| 14 #endif |
| 15 |
| 16 SK_DECLARE_STATIC_MUTEX(gPurgeableImageMutex); |
| 17 |
| 18 SkImageCache* SkPurgeableImageCache::GetPurgeableImageCache() { |
| 19 if (!SkPurgeableMemoryBlock::PlatformSupportsPurgeableMemory()) { |
| 20 return NULL; |
| 21 } |
| 22 SkAutoMutexAcquire ac(&gPurgeableImageMutex); |
| 23 static SkPurgeableImageCache gCache; |
| 24 gCache.ref(); |
| 25 return &gCache; |
| 26 } |
| 27 |
| 28 SkPurgeableImageCache::SkPurgeableImageCache() {} |
| 29 |
| 30 #ifdef SK_DEBUG |
| 31 SkPurgeableImageCache::~SkPurgeableImageCache() { |
| 32 SkASSERT(fRecs.count() == 0); |
| 33 } |
| 34 #endif |
| 35 |
| 36 |
| 37 void* SkPurgeableImageCache::allocAndPinCache(size_t bytes, intptr_t* ID) { |
| 38 SkAutoMutexAcquire ac(&gPurgeableImageMutex); |
| 39 |
| 40 SkPurgeableMemoryBlock* block = SkPurgeableMemoryBlock::CreatePurgeableMemor
yBlock(bytes); |
| 41 if (NULL == block) { |
| 42 return NULL; |
| 43 } |
| 44 |
| 45 SkASSERT(ID != NULL); |
| 46 *ID = reinterpret_cast<intptr_t>(block); |
| 47 #ifdef SK_DEBUG |
| 48 // Insert into the array of all recs: |
| 49 int index = this->findRec(*ID); |
| 50 SkASSERT(index < 0); |
| 51 fRecs.insert(~index, 1, ID); |
| 52 #endif |
| 53 return block->data(); |
| 54 } |
| 55 |
| 56 void* SkPurgeableImageCache::pinCache(intptr_t ID, SkImageCache::DataStatus* sta
tus) { |
| 57 SkASSERT(ID != SkImageCache::UNINITIALIZED_ID); |
| 58 SkAutoMutexAcquire ac(&gPurgeableImageMutex); |
| 59 |
| 60 SkASSERT(this->findRec(ID) >= 0); |
| 61 SkPurgeableMemoryBlock* block = reinterpret_cast<SkPurgeableMemoryBlock*>(ID
); |
| 62 SkPurgeableMemoryBlock::PinResult pinResult = block->pin(); |
| 63 switch (pinResult) { |
| 64 case SkPurgeableMemoryBlock::kFailed_PinResult: |
| 65 this->removeRec(ID); |
| 66 return NULL; |
| 67 |
| 68 case SkPurgeableMemoryBlock::kRetained_PinResult: |
| 69 *status = SkImageCache::kRetained_DataStatus; |
| 70 break; |
| 71 |
| 72 case SkPurgeableMemoryBlock::kUninitialized_PinResult: |
| 73 *status = SkImageCache::kUninitialized_DataStatus; |
| 74 break; |
| 75 |
| 76 default: |
| 77 // Invalid value. Treat as a failure to pin. |
| 78 SkASSERT(false); |
| 79 this->removeRec(ID); |
| 80 return NULL; |
| 81 } |
| 82 return block->data(); |
| 83 } |
| 84 |
| 85 void SkPurgeableImageCache::releaseCache(intptr_t ID) { |
| 86 SkASSERT(ID != SkImageCache::UNINITIALIZED_ID); |
| 87 SkAutoMutexAcquire ac(&gPurgeableImageMutex); |
| 88 |
| 89 SkASSERT(this->findRec(ID) >= 0); |
| 90 SkPurgeableMemoryBlock* block = reinterpret_cast<SkPurgeableMemoryBlock*>(ID
); |
| 91 block->unpin(); |
| 92 } |
| 93 |
| 94 void SkPurgeableImageCache::throwAwayCache(intptr_t ID) { |
| 95 SkASSERT(ID != SkImageCache::UNINITIALIZED_ID); |
| 96 SkAutoMutexAcquire ac(&gPurgeableImageMutex); |
| 97 |
| 98 this->removeRec(ID); |
| 99 } |
| 100 |
| 101 #ifdef SK_DEBUG |
| 102 SkImageCache::MemoryStatus SkPurgeableImageCache::getMemoryStatus(intptr_t ID) c
onst { |
| 103 SkAutoMutexAcquire ac(&gPurgeableImageMutex); |
| 104 if (SkImageCache::UNINITIALIZED_ID == ID || this->findRec(ID) < 0) { |
| 105 return SkImageCache::kFreed_MemoryStatus; |
| 106 } |
| 107 |
| 108 SkPurgeableMemoryBlock* block = reinterpret_cast<SkPurgeableMemoryBlock*>(ID
); |
| 109 #ifdef SK_BUILD_FOR_ANDROID |
| 110 if (-1 == block->getFD()) { |
| 111 return SkImageCache::kFreed_MemoryStatus; |
| 112 } |
| 113 #endif |
| 114 if (block->isPinned()) { |
| 115 return SkImageCache::kPinned_MemoryStatus; |
| 116 } |
| 117 return SkImageCache::kUnpinned_MemoryStatus; |
| 118 } |
| 119 |
| 120 void SkPurgeableImageCache::purgeAllUnpinnedCaches() { |
| 121 SkAutoMutexAcquire ac(&gPurgeableImageMutex); |
| 122 if (SkPurgeableMemoryBlock::PlatformSupportsPurgingAllUnpinnedBlocks()) { |
| 123 SkPurgeableMemoryBlock::PurgeAllUnpinnedBlocks(); |
| 124 } else { |
| 125 // Go through the blocks, and purge them individually. |
| 126 // Rather than deleting the blocks, which would interfere with further c
alls, purge them |
| 127 // and keep them around. |
| 128 for (int i = 0; i < fRecs.count(); i++) { |
| 129 SkPurgeableMemoryBlock* block = reinterpret_cast<SkPurgeableMemoryBl
ock*>(fRecs[i]); |
| 130 if (!block->isPinned()) { |
| 131 if (!block->purge()) { |
| 132 // FIXME: This should be more meaningful (which one, etc...) |
| 133 SkDebugf("Failed to purge\n"); |
| 134 } |
| 135 } |
| 136 } |
| 137 } |
| 138 } |
| 139 |
| 140 int SkPurgeableImageCache::findRec(intptr_t rec) const { |
| 141 return SkTSearch(fRecs.begin(), fRecs.count(), rec, sizeof(intptr_t)); |
| 142 } |
| 143 #endif |
| 144 |
| 145 void SkPurgeableImageCache::removeRec(intptr_t ID) { |
| 146 #ifdef SK_DEBUG |
| 147 int index = this->findRec(ID); |
| 148 SkASSERT(index >= 0); |
| 149 fRecs.remove(index); |
| 150 #endif |
| 151 SkPurgeableMemoryBlock* block = reinterpret_cast<SkPurgeableMemoryBlock*>(ID
); |
| 152 SkASSERT(!block->isPinned()); |
| 153 SkDELETE(block); |
| 154 } |
OLD | NEW |