reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 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 "SkScaledImageCache.h" |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 9 | #include "SkMipMap.h" |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 10 | #include "SkOnce.h" |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 11 | #include "SkPixelRef.h" |
| 12 | #include "SkRect.h" |
| 13 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 14 | // This can be defined by the caller's build system |
| 15 | //#define SK_USE_DISCARDABLE_SCALEDIMAGECACHE |
| 16 | |
| 17 | #ifndef SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT |
| 18 | # define SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT 1024 |
| 19 | #endif |
| 20 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 21 | #ifndef SK_DEFAULT_IMAGE_CACHE_LIMIT |
| 22 | #define SK_DEFAULT_IMAGE_CACHE_LIMIT (2 * 1024 * 1024) |
| 23 | #endif |
| 24 | |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 25 | static inline SkScaledImageCache::ID* rec_to_id(SkScaledImageCache::Rec* rec) { |
| 26 | return reinterpret_cast<SkScaledImageCache::ID*>(rec); |
| 27 | } |
| 28 | |
| 29 | static inline SkScaledImageCache::Rec* id_to_rec(SkScaledImageCache::ID* id) { |
| 30 | return reinterpret_cast<SkScaledImageCache::Rec*>(id); |
| 31 | } |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 32 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 33 | // Implemented from en.wikipedia.org/wiki/MurmurHash. |
| 34 | static uint32_t compute_hash(const uint32_t data[], int count) { |
| 35 | uint32_t hash = 0; |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 36 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 37 | for (int i = 0; i < count; ++i) { |
| 38 | uint32_t k = data[i]; |
| 39 | k *= 0xcc9e2d51; |
| 40 | k = (k << 15) | (k >> 17); |
| 41 | k *= 0x1b873593; |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 42 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 43 | hash ^= k; |
| 44 | hash = (hash << 13) | (hash >> 19); |
| 45 | hash *= 5; |
| 46 | hash += 0xe6546b64; |
| 47 | } |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 48 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 49 | // hash ^= size; |
| 50 | hash ^= hash >> 16; |
| 51 | hash *= 0x85ebca6b; |
| 52 | hash ^= hash >> 13; |
| 53 | hash *= 0xc2b2ae35; |
| 54 | hash ^= hash >> 16; |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 55 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 56 | return hash; |
| 57 | } |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 58 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 59 | struct SkScaledImageCache::Key { |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 60 | Key(uint32_t genID, |
| 61 | SkScalar scaleX, |
| 62 | SkScalar scaleY, |
| 63 | SkIRect bounds) |
| 64 | : fGenID(genID) |
| 65 | , fScaleX(scaleX) |
| 66 | , fScaleY(scaleY) |
| 67 | , fBounds(bounds) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 68 | fHash = compute_hash(&fGenID, 7); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 69 | } |
| 70 | |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 71 | bool operator<(const Key& other) const { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 72 | const uint32_t* a = &fGenID; |
| 73 | const uint32_t* b = &other.fGenID; |
| 74 | for (int i = 0; i < 7; ++i) { |
| 75 | if (a[i] < b[i]) { |
| 76 | return true; |
| 77 | } |
| 78 | if (a[i] > b[i]) { |
| 79 | return false; |
| 80 | } |
| 81 | } |
| 82 | return false; |
| 83 | } |
| 84 | |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 85 | bool operator==(const Key& other) const { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 86 | const uint32_t* a = &fHash; |
| 87 | const uint32_t* b = &other.fHash; |
| 88 | for (int i = 0; i < 8; ++i) { |
| 89 | if (a[i] != b[i]) { |
| 90 | return false; |
| 91 | } |
| 92 | } |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | uint32_t fHash; |
| 97 | uint32_t fGenID; |
| 98 | float fScaleX; |
| 99 | float fScaleY; |
| 100 | SkIRect fBounds; |
| 101 | }; |
| 102 | |
| 103 | struct SkScaledImageCache::Rec { |
| 104 | Rec(const Key& key, const SkBitmap& bm) : fKey(key), fBitmap(bm) { |
| 105 | fLockCount = 1; |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 106 | fMip = NULL; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 107 | } |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 108 | |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 109 | Rec(const Key& key, const SkMipMap* mip) : fKey(key) { |
| 110 | fLockCount = 1; |
| 111 | fMip = mip; |
| 112 | mip->ref(); |
| 113 | } |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 114 | |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 115 | ~Rec() { |
| 116 | SkSafeUnref(fMip); |
| 117 | } |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 118 | |
commit-bot@chromium.org | 55bd940 | 2014-04-02 19:17:00 +0000 | [diff] [blame] | 119 | static const Key& GetKey(const Rec& rec) { return rec.fKey; } |
| 120 | static uint32_t Hash(const Key& key) { return key.fHash; } |
| 121 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 122 | size_t bytesUsed() const { |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 123 | return fMip ? fMip->getSize() : fBitmap.getSize(); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | Rec* fNext; |
| 127 | Rec* fPrev; |
| 128 | |
| 129 | // this guy wants to be 64bit aligned |
| 130 | Key fKey; |
| 131 | |
| 132 | int32_t fLockCount; |
skia.committer@gmail.com | 5c561cb | 2013-07-25 07:01:00 +0000 | [diff] [blame] | 133 | |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 134 | // we use either fBitmap or fMip, but not both |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 135 | SkBitmap fBitmap; |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 136 | const SkMipMap* fMip; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 137 | }; |
| 138 | |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 139 | #include "SkTDynamicHash.h" |
| 140 | |
commit-bot@chromium.org | 55bd940 | 2014-04-02 19:17:00 +0000 | [diff] [blame] | 141 | class SkScaledImageCache::Hash : |
| 142 | public SkTDynamicHash<SkScaledImageCache::Rec, SkScaledImageCache::Key> {}; |
commit-bot@chromium.org | 158f646 | 2014-04-02 17:03:09 +0000 | [diff] [blame] | 143 | |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 144 | |
| 145 | /////////////////////////////////////////////////////////////////////////////// |
| 146 | |
reed@google.com | b8d17fe | 2013-07-25 17:42:24 +0000 | [diff] [blame] | 147 | // experimental hash to speed things up |
reed@google.com | ea348cb | 2013-07-25 17:50:37 +0000 | [diff] [blame] | 148 | #define USE_HASH |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 149 | |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 150 | #if !defined(USE_HASH) |
| 151 | static inline SkScaledImageCache::Rec* find_rec_in_list( |
| 152 | SkScaledImageCache::Rec* head, const Key & key) { |
| 153 | SkScaledImageCache::Rec* rec = head; |
| 154 | while ((rec != NULL) && (rec->fKey != key)) { |
| 155 | rec = rec->fNext; |
| 156 | } |
| 157 | return rec; |
| 158 | } |
| 159 | #endif |
| 160 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 161 | void SkScaledImageCache::init() { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 162 | fHead = NULL; |
| 163 | fTail = NULL; |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 164 | #ifdef USE_HASH |
| 165 | fHash = new Hash; |
| 166 | #else |
| 167 | fHash = NULL; |
| 168 | #endif |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 169 | fBytesUsed = 0; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 170 | fCount = 0; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 171 | fAllocator = NULL; |
| 172 | |
| 173 | // One of these should be explicit set by the caller after we return. |
| 174 | fByteLimit = 0; |
| 175 | fDiscardableFactory = NULL; |
| 176 | } |
| 177 | |
| 178 | #include "SkDiscardableMemory.h" |
| 179 | |
| 180 | class SkOneShotDiscardablePixelRef : public SkPixelRef { |
| 181 | public: |
commit-bot@chromium.org | 227c246 | 2014-01-24 18:33:07 +0000 | [diff] [blame] | 182 | SK_DECLARE_INST_COUNT(SkOneShotDiscardablePixelRef) |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 183 | // Ownership of the discardablememory is transfered to the pixelref |
| 184 | SkOneShotDiscardablePixelRef(const SkImageInfo&, SkDiscardableMemory*, size_t rowBytes); |
| 185 | ~SkOneShotDiscardablePixelRef(); |
| 186 | |
| 187 | SK_DECLARE_UNFLATTENABLE_OBJECT() |
| 188 | |
| 189 | protected: |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 190 | virtual bool onNewLockPixels(LockRec*) SK_OVERRIDE; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 191 | virtual void onUnlockPixels() SK_OVERRIDE; |
| 192 | virtual size_t getAllocatedSizeInBytes() const SK_OVERRIDE; |
| 193 | |
| 194 | private: |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 195 | SkDiscardableMemory* fDM; |
| 196 | size_t fRB; |
| 197 | bool fFirstTime; |
| 198 | |
| 199 | typedef SkPixelRef INHERITED; |
| 200 | }; |
| 201 | |
| 202 | SkOneShotDiscardablePixelRef::SkOneShotDiscardablePixelRef(const SkImageInfo& info, |
| 203 | SkDiscardableMemory* dm, |
| 204 | size_t rowBytes) |
| 205 | : INHERITED(info) |
| 206 | , fDM(dm) |
| 207 | , fRB(rowBytes) |
| 208 | { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 209 | SkASSERT(dm->data()); |
| 210 | fFirstTime = true; |
| 211 | } |
| 212 | |
| 213 | SkOneShotDiscardablePixelRef::~SkOneShotDiscardablePixelRef() { |
| 214 | SkDELETE(fDM); |
| 215 | } |
| 216 | |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 217 | bool SkOneShotDiscardablePixelRef::onNewLockPixels(LockRec* rec) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 218 | if (fFirstTime) { |
| 219 | // we're already locked |
reed@google.com | c83a91f | 2013-12-13 13:41:14 +0000 | [diff] [blame] | 220 | SkASSERT(fDM->data()); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 221 | fFirstTime = false; |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 222 | goto SUCCESS; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 223 | } |
commit-bot@chromium.org | 281713e | 2013-12-12 18:08:08 +0000 | [diff] [blame] | 224 | |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 225 | // A previous call to onUnlock may have deleted our DM, so check for that |
| 226 | if (NULL == fDM) { |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 227 | return false; |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | if (!fDM->lock()) { |
| 231 | // since it failed, we delete it now, to free-up the resource |
| 232 | delete fDM; |
| 233 | fDM = NULL; |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 234 | return false; |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 235 | } |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 236 | |
| 237 | SUCCESS: |
| 238 | rec->fPixels = fDM->data(); |
| 239 | rec->fColorTable = NULL; |
| 240 | rec->fRowBytes = fRB; |
| 241 | return true; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | void SkOneShotDiscardablePixelRef::onUnlockPixels() { |
| 245 | SkASSERT(!fFirstTime); |
reed@google.com | c83a91f | 2013-12-13 13:41:14 +0000 | [diff] [blame] | 246 | fDM->unlock(); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | size_t SkOneShotDiscardablePixelRef::getAllocatedSizeInBytes() const { |
reed@google.com | d0419b1 | 2014-01-06 17:08:27 +0000 | [diff] [blame] | 250 | return this->info().getSafeSize(fRB); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | class SkScaledImageCacheDiscardableAllocator : public SkBitmap::Allocator { |
| 254 | public: |
| 255 | SkScaledImageCacheDiscardableAllocator( |
| 256 | SkScaledImageCache::DiscardableFactory factory) { |
| 257 | SkASSERT(factory); |
| 258 | fFactory = factory; |
| 259 | } |
| 260 | |
| 261 | virtual bool allocPixelRef(SkBitmap*, SkColorTable*) SK_OVERRIDE; |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 262 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 263 | private: |
| 264 | SkScaledImageCache::DiscardableFactory fFactory; |
| 265 | }; |
| 266 | |
| 267 | bool SkScaledImageCacheDiscardableAllocator::allocPixelRef(SkBitmap* bitmap, |
| 268 | SkColorTable* ctable) { |
| 269 | size_t size = bitmap->getSize(); |
| 270 | if (0 == size) { |
| 271 | return false; |
| 272 | } |
| 273 | |
| 274 | SkDiscardableMemory* dm = fFactory(size); |
| 275 | if (NULL == dm) { |
| 276 | return false; |
| 277 | } |
| 278 | |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 279 | // can we relax this? |
commit-bot@chromium.org | 149e9a1 | 2014-04-09 20:45:29 +0000 | [diff] [blame^] | 280 | if (kN32_SkColorType != bitmap->colorType()) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 281 | return false; |
| 282 | } |
| 283 | |
reed@google.com | 900ecf2 | 2014-02-20 20:55:37 +0000 | [diff] [blame] | 284 | SkImageInfo info = bitmap->info(); |
commit-bot@chromium.org | e13af71 | 2014-01-13 20:39:14 +0000 | [diff] [blame] | 285 | bitmap->setPixelRef(SkNEW_ARGS(SkOneShotDiscardablePixelRef, |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 286 | (info, dm, bitmap->rowBytes())))->unref(); |
| 287 | bitmap->lockPixels(); |
| 288 | return bitmap->readyToDraw(); |
| 289 | } |
| 290 | |
| 291 | SkScaledImageCache::SkScaledImageCache(DiscardableFactory factory) { |
| 292 | this->init(); |
| 293 | fDiscardableFactory = factory; |
| 294 | |
| 295 | fAllocator = SkNEW_ARGS(SkScaledImageCacheDiscardableAllocator, (factory)); |
| 296 | } |
| 297 | |
| 298 | SkScaledImageCache::SkScaledImageCache(size_t byteLimit) { |
| 299 | this->init(); |
| 300 | fByteLimit = byteLimit; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | SkScaledImageCache::~SkScaledImageCache() { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 304 | SkSafeUnref(fAllocator); |
| 305 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 306 | Rec* rec = fHead; |
| 307 | while (rec) { |
| 308 | Rec* next = rec->fNext; |
| 309 | SkDELETE(rec); |
| 310 | rec = next; |
| 311 | } |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 312 | delete fHash; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 313 | } |
| 314 | |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 315 | //////////////////////////////////////////////////////////////////////////////// |
| 316 | |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 317 | |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 318 | SkScaledImageCache::Rec* SkScaledImageCache::findAndLock(uint32_t genID, |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 319 | SkScalar scaleX, |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 320 | SkScalar scaleY, |
| 321 | const SkIRect& bounds) { |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 322 | const Key key(genID, scaleX, scaleY, bounds); |
| 323 | return this->findAndLock(key); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | This private method is the fully general record finder. All other |
| 328 | record finders should call this function or the one above. */ |
| 329 | SkScaledImageCache::Rec* SkScaledImageCache::findAndLock(const SkScaledImageCache::Key& key) { |
| 330 | if (key.fBounds.isEmpty()) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 331 | return NULL; |
| 332 | } |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 333 | #ifdef USE_HASH |
| 334 | Rec* rec = fHash->find(key); |
| 335 | #else |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 336 | Rec* rec = find_rec_in_list(fHead, key); |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 337 | #endif |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 338 | if (rec) { |
| 339 | this->moveToHead(rec); // for our LRU |
| 340 | rec->fLockCount += 1; |
| 341 | } |
| 342 | return rec; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 343 | } |
| 344 | |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 345 | /** |
| 346 | This function finds the bounds of the bitmap *within its pixelRef*. |
| 347 | If the bitmap lacks a pixelRef, it will return an empty rect, since |
| 348 | that doesn't make sense. This may be a useful enough function that |
| 349 | it should be somewhere else (in SkBitmap?). */ |
| 350 | static SkIRect get_bounds_from_bitmap(const SkBitmap& bm) { |
| 351 | if (!(bm.pixelRef())) { |
| 352 | return SkIRect::MakeEmpty(); |
| 353 | } |
reed@google.com | 672588b | 2014-01-08 15:42:01 +0000 | [diff] [blame] | 354 | SkIPoint origin = bm.pixelRefOrigin(); |
| 355 | return SkIRect::MakeXYWH(origin.fX, origin.fY, bm.width(), bm.height()); |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | |
| 359 | SkScaledImageCache::ID* SkScaledImageCache::findAndLock(uint32_t genID, |
| 360 | int32_t width, |
| 361 | int32_t height, |
| 362 | SkBitmap* bitmap) { |
| 363 | Rec* rec = this->findAndLock(genID, SK_Scalar1, SK_Scalar1, |
| 364 | SkIRect::MakeWH(width, height)); |
| 365 | if (rec) { |
| 366 | SkASSERT(NULL == rec->fMip); |
| 367 | SkASSERT(rec->fBitmap.pixelRef()); |
| 368 | *bitmap = rec->fBitmap; |
| 369 | } |
| 370 | return rec_to_id(rec); |
| 371 | } |
| 372 | |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 373 | SkScaledImageCache::ID* SkScaledImageCache::findAndLock(const SkBitmap& orig, |
| 374 | SkScalar scaleX, |
| 375 | SkScalar scaleY, |
| 376 | SkBitmap* scaled) { |
| 377 | if (0 == scaleX || 0 == scaleY) { |
| 378 | // degenerate, and the key we use for mipmaps |
| 379 | return NULL; |
| 380 | } |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 381 | Rec* rec = this->findAndLock(orig.getGenerationID(), scaleX, |
| 382 | scaleY, get_bounds_from_bitmap(orig)); |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 383 | if (rec) { |
| 384 | SkASSERT(NULL == rec->fMip); |
| 385 | SkASSERT(rec->fBitmap.pixelRef()); |
| 386 | *scaled = rec->fBitmap; |
| 387 | } |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 388 | return rec_to_id(rec); |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | SkScaledImageCache::ID* SkScaledImageCache::findAndLockMip(const SkBitmap& orig, |
| 392 | SkMipMap const ** mip) { |
skia.committer@gmail.com | b77f0f4 | 2013-10-30 07:01:56 +0000 | [diff] [blame] | 393 | Rec* rec = this->findAndLock(orig.getGenerationID(), 0, 0, |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 394 | get_bounds_from_bitmap(orig)); |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 395 | if (rec) { |
| 396 | SkASSERT(rec->fMip); |
| 397 | SkASSERT(NULL == rec->fBitmap.pixelRef()); |
| 398 | *mip = rec->fMip; |
| 399 | } |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 400 | return rec_to_id(rec); |
| 401 | } |
| 402 | |
| 403 | |
| 404 | //////////////////////////////////////////////////////////////////////////////// |
| 405 | /** |
| 406 | This private method is the fully general record adder. All other |
| 407 | record adders should call this funtion. */ |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 408 | SkScaledImageCache::ID* SkScaledImageCache::addAndLock(SkScaledImageCache::Rec* rec) { |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 409 | SkASSERT(rec); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 410 | // See if we already have this key (racy inserts, etc.) |
| 411 | Rec* existing = this->findAndLock(rec->fKey); |
robertphillips@google.com | 5db04df | 2013-12-18 18:48:08 +0000 | [diff] [blame] | 412 | if (NULL != existing) { |
| 413 | // Since we already have a matching entry, just delete the new one and return. |
| 414 | // Call sites cannot assume the passed in object will live past this call. |
commit-bot@chromium.org | 5e4112b | 2014-03-05 13:44:18 +0000 | [diff] [blame] | 415 | existing->fBitmap = rec->fBitmap; |
robertphillips@google.com | 5db04df | 2013-12-18 18:48:08 +0000 | [diff] [blame] | 416 | SkDELETE(rec); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 417 | return rec_to_id(existing); |
| 418 | } |
| 419 | |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 420 | this->addToHead(rec); |
| 421 | SkASSERT(1 == rec->fLockCount); |
| 422 | #ifdef USE_HASH |
| 423 | SkASSERT(fHash); |
| 424 | fHash->add(rec); |
| 425 | #endif |
| 426 | // We may (now) be overbudget, so see if we need to purge something. |
| 427 | this->purgeAsNeeded(); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 428 | return rec_to_id(rec); |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | SkScaledImageCache::ID* SkScaledImageCache::addAndLock(uint32_t genID, |
| 432 | int32_t width, |
| 433 | int32_t height, |
| 434 | const SkBitmap& bitmap) { |
| 435 | Key key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeWH(width, height)); |
| 436 | Rec* rec = SkNEW_ARGS(Rec, (key, bitmap)); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 437 | return this->addAndLock(rec); |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 438 | } |
| 439 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 440 | SkScaledImageCache::ID* SkScaledImageCache::addAndLock(const SkBitmap& orig, |
| 441 | SkScalar scaleX, |
| 442 | SkScalar scaleY, |
| 443 | const SkBitmap& scaled) { |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 444 | if (0 == scaleX || 0 == scaleY) { |
| 445 | // degenerate, and the key we use for mipmaps |
| 446 | return NULL; |
| 447 | } |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 448 | SkIRect bounds = get_bounds_from_bitmap(orig); |
| 449 | if (bounds.isEmpty()) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 450 | return NULL; |
| 451 | } |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 452 | Key key(orig.getGenerationID(), scaleX, scaleY, bounds); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 453 | Rec* rec = SkNEW_ARGS(Rec, (key, scaled)); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 454 | return this->addAndLock(rec); |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 455 | } |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 456 | |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 457 | SkScaledImageCache::ID* SkScaledImageCache::addAndLockMip(const SkBitmap& orig, |
| 458 | const SkMipMap* mip) { |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 459 | SkIRect bounds = get_bounds_from_bitmap(orig); |
| 460 | if (bounds.isEmpty()) { |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 461 | return NULL; |
| 462 | } |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 463 | Key key(orig.getGenerationID(), 0, 0, bounds); |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 464 | Rec* rec = SkNEW_ARGS(Rec, (key, mip)); |
rmistry@google.com | d6bab02 | 2013-12-02 13:50:38 +0000 | [diff] [blame] | 465 | return this->addAndLock(rec); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | void SkScaledImageCache::unlock(SkScaledImageCache::ID* id) { |
| 469 | SkASSERT(id); |
| 470 | |
| 471 | #ifdef SK_DEBUG |
| 472 | { |
| 473 | bool found = false; |
| 474 | Rec* rec = fHead; |
| 475 | while (rec != NULL) { |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 476 | if (rec == id_to_rec(id)) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 477 | found = true; |
| 478 | break; |
| 479 | } |
| 480 | rec = rec->fNext; |
| 481 | } |
| 482 | SkASSERT(found); |
| 483 | } |
| 484 | #endif |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 485 | Rec* rec = id_to_rec(id); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 486 | SkASSERT(rec->fLockCount > 0); |
| 487 | rec->fLockCount -= 1; |
| 488 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 489 | // we may have been over-budget, but now have released something, so check |
| 490 | // if we should purge. |
| 491 | if (0 == rec->fLockCount) { |
| 492 | this->purgeAsNeeded(); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | void SkScaledImageCache::purgeAsNeeded() { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 497 | size_t byteLimit; |
| 498 | int countLimit; |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 499 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 500 | if (fDiscardableFactory) { |
| 501 | countLimit = SK_DISCARDABLEMEMORY_SCALEDIMAGECACHE_COUNT_LIMIT; |
| 502 | byteLimit = SK_MaxU32; // no limit based on bytes |
| 503 | } else { |
| 504 | countLimit = SK_MaxS32; // no limit based on count |
| 505 | byteLimit = fByteLimit; |
| 506 | } |
| 507 | |
| 508 | size_t bytesUsed = fBytesUsed; |
| 509 | int countUsed = fCount; |
skia.committer@gmail.com | cf0803b | 2013-12-10 07:02:03 +0000 | [diff] [blame] | 510 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 511 | Rec* rec = fTail; |
| 512 | while (rec) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 513 | if (bytesUsed < byteLimit && countUsed < countLimit) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 514 | break; |
| 515 | } |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 516 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 517 | Rec* prev = rec->fPrev; |
| 518 | if (0 == rec->fLockCount) { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 519 | size_t used = rec->bytesUsed(); |
| 520 | SkASSERT(used <= bytesUsed); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 521 | this->detach(rec); |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 522 | #ifdef USE_HASH |
| 523 | fHash->remove(rec->fKey); |
| 524 | #endif |
skia.committer@gmail.com | 956b310 | 2013-07-26 07:00:58 +0000 | [diff] [blame] | 525 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 526 | SkDELETE(rec); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 527 | |
| 528 | bytesUsed -= used; |
| 529 | countUsed -= 1; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 530 | } |
| 531 | rec = prev; |
| 532 | } |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 533 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 534 | fBytesUsed = bytesUsed; |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 535 | fCount = countUsed; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| 538 | size_t SkScaledImageCache::setByteLimit(size_t newLimit) { |
| 539 | size_t prevLimit = fByteLimit; |
| 540 | fByteLimit = newLimit; |
| 541 | if (newLimit < prevLimit) { |
| 542 | this->purgeAsNeeded(); |
| 543 | } |
| 544 | return prevLimit; |
| 545 | } |
| 546 | |
| 547 | /////////////////////////////////////////////////////////////////////////////// |
| 548 | |
| 549 | void SkScaledImageCache::detach(Rec* rec) { |
| 550 | Rec* prev = rec->fPrev; |
| 551 | Rec* next = rec->fNext; |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 552 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 553 | if (!prev) { |
| 554 | SkASSERT(fHead == rec); |
| 555 | fHead = next; |
| 556 | } else { |
| 557 | prev->fNext = next; |
| 558 | } |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 559 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 560 | if (!next) { |
| 561 | fTail = prev; |
| 562 | } else { |
| 563 | next->fPrev = prev; |
| 564 | } |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 565 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 566 | rec->fNext = rec->fPrev = NULL; |
| 567 | } |
| 568 | |
| 569 | void SkScaledImageCache::moveToHead(Rec* rec) { |
| 570 | if (fHead == rec) { |
| 571 | return; |
| 572 | } |
| 573 | |
| 574 | SkASSERT(fHead); |
| 575 | SkASSERT(fTail); |
| 576 | |
| 577 | this->validate(); |
| 578 | |
| 579 | this->detach(rec); |
| 580 | |
| 581 | fHead->fPrev = rec; |
| 582 | rec->fNext = fHead; |
| 583 | fHead = rec; |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 584 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 585 | this->validate(); |
| 586 | } |
| 587 | |
| 588 | void SkScaledImageCache::addToHead(Rec* rec) { |
| 589 | this->validate(); |
| 590 | |
| 591 | rec->fPrev = NULL; |
| 592 | rec->fNext = fHead; |
| 593 | if (fHead) { |
| 594 | fHead->fPrev = rec; |
| 595 | } |
| 596 | fHead = rec; |
| 597 | if (!fTail) { |
| 598 | fTail = rec; |
| 599 | } |
| 600 | fBytesUsed += rec->bytesUsed(); |
| 601 | fCount += 1; |
| 602 | |
| 603 | this->validate(); |
| 604 | } |
| 605 | |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 606 | /////////////////////////////////////////////////////////////////////////////// |
| 607 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 608 | #ifdef SK_DEBUG |
| 609 | void SkScaledImageCache::validate() const { |
| 610 | if (NULL == fHead) { |
| 611 | SkASSERT(NULL == fTail); |
| 612 | SkASSERT(0 == fBytesUsed); |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | if (fHead == fTail) { |
| 617 | SkASSERT(NULL == fHead->fPrev); |
| 618 | SkASSERT(NULL == fHead->fNext); |
| 619 | SkASSERT(fHead->bytesUsed() == fBytesUsed); |
| 620 | return; |
| 621 | } |
| 622 | |
| 623 | SkASSERT(NULL == fHead->fPrev); |
| 624 | SkASSERT(NULL != fHead->fNext); |
| 625 | SkASSERT(NULL == fTail->fNext); |
| 626 | SkASSERT(NULL != fTail->fPrev); |
| 627 | |
| 628 | size_t used = 0; |
| 629 | int count = 0; |
| 630 | const Rec* rec = fHead; |
| 631 | while (rec) { |
| 632 | count += 1; |
| 633 | used += rec->bytesUsed(); |
| 634 | SkASSERT(used <= fBytesUsed); |
| 635 | rec = rec->fNext; |
| 636 | } |
| 637 | SkASSERT(fCount == count); |
| 638 | |
| 639 | rec = fTail; |
| 640 | while (rec) { |
| 641 | SkASSERT(count > 0); |
| 642 | count -= 1; |
| 643 | SkASSERT(used >= rec->bytesUsed()); |
| 644 | used -= rec->bytesUsed(); |
| 645 | rec = rec->fPrev; |
| 646 | } |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 647 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 648 | SkASSERT(0 == count); |
| 649 | SkASSERT(0 == used); |
| 650 | } |
| 651 | #endif |
| 652 | |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 653 | void SkScaledImageCache::dump() const { |
| 654 | this->validate(); |
| 655 | |
| 656 | const Rec* rec = fHead; |
| 657 | int locked = 0; |
| 658 | while (rec) { |
| 659 | locked += rec->fLockCount > 0; |
| 660 | rec = rec->fNext; |
| 661 | } |
| 662 | |
| 663 | SkDebugf("SkScaledImageCache: count=%d bytes=%d locked=%d %s\n", |
| 664 | fCount, fBytesUsed, locked, |
| 665 | fDiscardableFactory ? "discardable" : "malloc"); |
| 666 | } |
| 667 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 668 | /////////////////////////////////////////////////////////////////////////////// |
| 669 | |
| 670 | #include "SkThread.h" |
| 671 | |
reed@google.com | be19dbe | 2013-07-24 15:06:34 +0000 | [diff] [blame] | 672 | SK_DECLARE_STATIC_MUTEX(gMutex); |
commit-bot@chromium.org | 709ca75 | 2014-01-24 22:38:39 +0000 | [diff] [blame] | 673 | static SkScaledImageCache* gScaledImageCache = NULL; |
| 674 | static void cleanup_gScaledImageCache() { SkDELETE(gScaledImageCache); } |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 675 | |
commit-bot@chromium.org | 709ca75 | 2014-01-24 22:38:39 +0000 | [diff] [blame] | 676 | static void create_cache(int) { |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 677 | #ifdef SK_USE_DISCARDABLE_SCALEDIMAGECACHE |
commit-bot@chromium.org | 709ca75 | 2014-01-24 22:38:39 +0000 | [diff] [blame] | 678 | gScaledImageCache = SkNEW_ARGS(SkScaledImageCache, (SkDiscardableMemory::Create)); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 679 | #else |
commit-bot@chromium.org | 709ca75 | 2014-01-24 22:38:39 +0000 | [diff] [blame] | 680 | gScaledImageCache = SkNEW_ARGS(SkScaledImageCache, (SK_DEFAULT_IMAGE_CACHE_LIMIT)); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 681 | #endif |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 682 | } |
| 683 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 684 | static SkScaledImageCache* get_cache() { |
commit-bot@chromium.org | 709ca75 | 2014-01-24 22:38:39 +0000 | [diff] [blame] | 685 | SK_DECLARE_STATIC_ONCE(once); |
| 686 | SkOnce(&once, create_cache, 0, cleanup_gScaledImageCache); |
| 687 | SkASSERT(NULL != gScaledImageCache); |
| 688 | return gScaledImageCache; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 689 | } |
| 690 | |
commit-bot@chromium.org | 7585479 | 2013-10-29 19:55:00 +0000 | [diff] [blame] | 691 | |
| 692 | SkScaledImageCache::ID* SkScaledImageCache::FindAndLock( |
| 693 | uint32_t pixelGenerationID, |
| 694 | int32_t width, |
| 695 | int32_t height, |
| 696 | SkBitmap* scaled) { |
| 697 | SkAutoMutexAcquire am(gMutex); |
| 698 | return get_cache()->findAndLock(pixelGenerationID, width, height, scaled); |
| 699 | } |
| 700 | |
| 701 | SkScaledImageCache::ID* SkScaledImageCache::AddAndLock( |
| 702 | uint32_t pixelGenerationID, |
| 703 | int32_t width, |
| 704 | int32_t height, |
| 705 | const SkBitmap& scaled) { |
| 706 | SkAutoMutexAcquire am(gMutex); |
| 707 | return get_cache()->addAndLock(pixelGenerationID, width, height, scaled); |
| 708 | } |
| 709 | |
| 710 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 711 | SkScaledImageCache::ID* SkScaledImageCache::FindAndLock(const SkBitmap& orig, |
| 712 | SkScalar scaleX, |
| 713 | SkScalar scaleY, |
| 714 | SkBitmap* scaled) { |
| 715 | SkAutoMutexAcquire am(gMutex); |
| 716 | return get_cache()->findAndLock(orig, scaleX, scaleY, scaled); |
| 717 | } |
| 718 | |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 719 | SkScaledImageCache::ID* SkScaledImageCache::FindAndLockMip(const SkBitmap& orig, |
| 720 | SkMipMap const ** mip) { |
| 721 | SkAutoMutexAcquire am(gMutex); |
| 722 | return get_cache()->findAndLockMip(orig, mip); |
| 723 | } |
| 724 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 725 | SkScaledImageCache::ID* SkScaledImageCache::AddAndLock(const SkBitmap& orig, |
| 726 | SkScalar scaleX, |
| 727 | SkScalar scaleY, |
| 728 | const SkBitmap& scaled) { |
| 729 | SkAutoMutexAcquire am(gMutex); |
| 730 | return get_cache()->addAndLock(orig, scaleX, scaleY, scaled); |
| 731 | } |
| 732 | |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 733 | SkScaledImageCache::ID* SkScaledImageCache::AddAndLockMip(const SkBitmap& orig, |
| 734 | const SkMipMap* mip) { |
| 735 | SkAutoMutexAcquire am(gMutex); |
| 736 | return get_cache()->addAndLockMip(orig, mip); |
| 737 | } |
| 738 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 739 | void SkScaledImageCache::Unlock(SkScaledImageCache::ID* id) { |
| 740 | SkAutoMutexAcquire am(gMutex); |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 741 | get_cache()->unlock(id); |
| 742 | |
| 743 | // get_cache()->dump(); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | size_t SkScaledImageCache::GetBytesUsed() { |
| 747 | SkAutoMutexAcquire am(gMutex); |
| 748 | return get_cache()->getBytesUsed(); |
| 749 | } |
| 750 | |
| 751 | size_t SkScaledImageCache::GetByteLimit() { |
| 752 | SkAutoMutexAcquire am(gMutex); |
| 753 | return get_cache()->getByteLimit(); |
| 754 | } |
| 755 | |
| 756 | size_t SkScaledImageCache::SetByteLimit(size_t newLimit) { |
| 757 | SkAutoMutexAcquire am(gMutex); |
| 758 | return get_cache()->setByteLimit(newLimit); |
| 759 | } |
| 760 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 761 | SkBitmap::Allocator* SkScaledImageCache::GetAllocator() { |
| 762 | SkAutoMutexAcquire am(gMutex); |
| 763 | return get_cache()->allocator(); |
| 764 | } |
| 765 | |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 766 | void SkScaledImageCache::Dump() { |
| 767 | SkAutoMutexAcquire am(gMutex); |
| 768 | get_cache()->dump(); |
| 769 | } |
| 770 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 771 | /////////////////////////////////////////////////////////////////////////////// |
| 772 | |
| 773 | #include "SkGraphics.h" |
| 774 | |
| 775 | size_t SkGraphics::GetImageCacheBytesUsed() { |
| 776 | return SkScaledImageCache::GetBytesUsed(); |
| 777 | } |
| 778 | |
| 779 | size_t SkGraphics::GetImageCacheByteLimit() { |
| 780 | return SkScaledImageCache::GetByteLimit(); |
| 781 | } |
| 782 | |
| 783 | size_t SkGraphics::SetImageCacheByteLimit(size_t newLimit) { |
| 784 | return SkScaledImageCache::SetByteLimit(newLimit); |
| 785 | } |