blob: 644ce7f05f6d72d4e8c52970103f67e4e2e2e9e9 [file] [log] [blame]
reed@google.com602a1d72013-07-23 19:13:54 +00001/*
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.comd94697c2013-07-24 14:31:33 +00009#include "SkMipMap.h"
commit-bot@chromium.org75854792013-10-29 19:55:00 +000010#include "SkOnce.h"
reed@google.com602a1d72013-07-23 19:13:54 +000011#include "SkPixelRef.h"
12#include "SkRect.h"
13
14#ifndef SK_DEFAULT_IMAGE_CACHE_LIMIT
15 #define SK_DEFAULT_IMAGE_CACHE_LIMIT (2 * 1024 * 1024)
16#endif
17
commit-bot@chromium.org75854792013-10-29 19:55:00 +000018static inline SkScaledImageCache::ID* rec_to_id(SkScaledImageCache::Rec* rec) {
19 return reinterpret_cast<SkScaledImageCache::ID*>(rec);
20}
21
22static inline SkScaledImageCache::Rec* id_to_rec(SkScaledImageCache::ID* id) {
23 return reinterpret_cast<SkScaledImageCache::Rec*>(id);
24}
reed@google.com5d1e5582013-07-25 14:36:15 +000025
reed@google.com602a1d72013-07-23 19:13:54 +000026 // Implemented from en.wikipedia.org/wiki/MurmurHash.
27static uint32_t compute_hash(const uint32_t data[], int count) {
28 uint32_t hash = 0;
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +000029
reed@google.com602a1d72013-07-23 19:13:54 +000030 for (int i = 0; i < count; ++i) {
31 uint32_t k = data[i];
32 k *= 0xcc9e2d51;
33 k = (k << 15) | (k >> 17);
34 k *= 0x1b873593;
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +000035
reed@google.com602a1d72013-07-23 19:13:54 +000036 hash ^= k;
37 hash = (hash << 13) | (hash >> 19);
38 hash *= 5;
39 hash += 0xe6546b64;
40 }
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +000041
reed@google.com602a1d72013-07-23 19:13:54 +000042 // hash ^= size;
43 hash ^= hash >> 16;
44 hash *= 0x85ebca6b;
45 hash ^= hash >> 13;
46 hash *= 0xc2b2ae35;
47 hash ^= hash >> 16;
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +000048
reed@google.com602a1d72013-07-23 19:13:54 +000049 return hash;
50}
reed@google.com602a1d72013-07-23 19:13:54 +000051
52struct Key {
commit-bot@chromium.org75854792013-10-29 19:55:00 +000053 Key(uint32_t genID,
54 SkScalar scaleX,
55 SkScalar scaleY,
56 SkIRect bounds)
57 : fGenID(genID)
58 , fScaleX(scaleX)
59 , fScaleY(scaleY)
60 , fBounds(bounds) {
reed@google.com602a1d72013-07-23 19:13:54 +000061 fHash = compute_hash(&fGenID, 7);
reed@google.com602a1d72013-07-23 19:13:54 +000062 }
63
reed@google.com5d1e5582013-07-25 14:36:15 +000064 bool operator<(const Key& other) const {
reed@google.com602a1d72013-07-23 19:13:54 +000065 const uint32_t* a = &fGenID;
66 const uint32_t* b = &other.fGenID;
67 for (int i = 0; i < 7; ++i) {
68 if (a[i] < b[i]) {
69 return true;
70 }
71 if (a[i] > b[i]) {
72 return false;
73 }
74 }
75 return false;
76 }
77
reed@google.com5d1e5582013-07-25 14:36:15 +000078 bool operator==(const Key& other) const {
reed@google.com602a1d72013-07-23 19:13:54 +000079 const uint32_t* a = &fHash;
80 const uint32_t* b = &other.fHash;
81 for (int i = 0; i < 8; ++i) {
82 if (a[i] != b[i]) {
83 return false;
84 }
85 }
86 return true;
87 }
88
89 uint32_t fHash;
90 uint32_t fGenID;
91 float fScaleX;
92 float fScaleY;
93 SkIRect fBounds;
94};
95
96struct SkScaledImageCache::Rec {
97 Rec(const Key& key, const SkBitmap& bm) : fKey(key), fBitmap(bm) {
98 fLockCount = 1;
reed@google.comd94697c2013-07-24 14:31:33 +000099 fMip = NULL;
reed@google.com602a1d72013-07-23 19:13:54 +0000100 }
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000101
reed@google.comd94697c2013-07-24 14:31:33 +0000102 Rec(const Key& key, const SkMipMap* mip) : fKey(key) {
103 fLockCount = 1;
104 fMip = mip;
105 mip->ref();
106 }
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000107
reed@google.comd94697c2013-07-24 14:31:33 +0000108 ~Rec() {
109 SkSafeUnref(fMip);
110 }
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000111
reed@google.com602a1d72013-07-23 19:13:54 +0000112 size_t bytesUsed() const {
reed@google.comd94697c2013-07-24 14:31:33 +0000113 return fMip ? fMip->getSize() : fBitmap.getSize();
reed@google.com602a1d72013-07-23 19:13:54 +0000114 }
115
116 Rec* fNext;
117 Rec* fPrev;
118
119 // this guy wants to be 64bit aligned
120 Key fKey;
121
122 int32_t fLockCount;
skia.committer@gmail.com5c561cb2013-07-25 07:01:00 +0000123
reed@google.comd94697c2013-07-24 14:31:33 +0000124 // we use either fBitmap or fMip, but not both
reed@google.com602a1d72013-07-23 19:13:54 +0000125 SkBitmap fBitmap;
reed@google.comd94697c2013-07-24 14:31:33 +0000126 const SkMipMap* fMip;
reed@google.com602a1d72013-07-23 19:13:54 +0000127};
128
reed@google.com5d1e5582013-07-25 14:36:15 +0000129#include "SkTDynamicHash.h"
130
reed@google.com56172672013-07-25 14:46:22 +0000131namespace { // can't use static functions w/ template parameters
132const Key& key_from_rec(const SkScaledImageCache::Rec& rec) {
reed@google.com5d1e5582013-07-25 14:36:15 +0000133 return rec.fKey;
134}
135
reed@google.com56172672013-07-25 14:46:22 +0000136uint32_t hash_from_key(const Key& key) {
reed@google.com5d1e5582013-07-25 14:36:15 +0000137 return key.fHash;
138}
139
reed@google.com56172672013-07-25 14:46:22 +0000140bool eq_rec_key(const SkScaledImageCache::Rec& rec, const Key& key) {
reed@google.com5d1e5582013-07-25 14:36:15 +0000141 return rec.fKey == key;
142}
reed@google.com56172672013-07-25 14:46:22 +0000143}
reed@google.com5d1e5582013-07-25 14:36:15 +0000144
145class SkScaledImageCache::Hash : public SkTDynamicHash<SkScaledImageCache::Rec,
146 Key, key_from_rec, hash_from_key,
147 eq_rec_key> {};
148
149///////////////////////////////////////////////////////////////////////////////
150
reed@google.comb8d17fe2013-07-25 17:42:24 +0000151// experimental hash to speed things up
reed@google.comea348cb2013-07-25 17:50:37 +0000152#define USE_HASH
reed@google.com5d1e5582013-07-25 14:36:15 +0000153
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000154#if !defined(USE_HASH)
155static inline SkScaledImageCache::Rec* find_rec_in_list(
156 SkScaledImageCache::Rec* head, const Key & key) {
157 SkScaledImageCache::Rec* rec = head;
158 while ((rec != NULL) && (rec->fKey != key)) {
159 rec = rec->fNext;
160 }
161 return rec;
162}
163#endif
164
reed@google.com602a1d72013-07-23 19:13:54 +0000165SkScaledImageCache::SkScaledImageCache(size_t byteLimit) {
166 fHead = NULL;
167 fTail = NULL;
reed@google.com5d1e5582013-07-25 14:36:15 +0000168#ifdef USE_HASH
169 fHash = new Hash;
170#else
171 fHash = NULL;
172#endif
reed@google.com602a1d72013-07-23 19:13:54 +0000173 fBytesUsed = 0;
174 fByteLimit = byteLimit;
175 fCount = 0;
176}
177
178SkScaledImageCache::~SkScaledImageCache() {
179 Rec* rec = fHead;
180 while (rec) {
181 Rec* next = rec->fNext;
182 SkDELETE(rec);
183 rec = next;
184 }
reed@google.com5d1e5582013-07-25 14:36:15 +0000185 delete fHash;
reed@google.com602a1d72013-07-23 19:13:54 +0000186}
187
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000188////////////////////////////////////////////////////////////////////////////////
189
190/**
191 This private method is the fully general record finder. All other
192 record finders should call this funtion. */
193SkScaledImageCache::Rec* SkScaledImageCache::findAndLock(uint32_t genID,
reed@google.com602a1d72013-07-23 19:13:54 +0000194 SkScalar scaleX,
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000195 SkScalar scaleY,
196 const SkIRect& bounds) {
197 if (bounds.isEmpty()) {
reed@google.com602a1d72013-07-23 19:13:54 +0000198 return NULL;
199 }
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000200 Key key(genID, scaleX, scaleY, bounds);
reed@google.com5d1e5582013-07-25 14:36:15 +0000201#ifdef USE_HASH
202 Rec* rec = fHash->find(key);
203#else
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000204 Rec* rec = find_rec_in_list(fHead, key);
reed@google.com5d1e5582013-07-25 14:36:15 +0000205#endif
reed@google.com5d1e5582013-07-25 14:36:15 +0000206 if (rec) {
207 this->moveToHead(rec); // for our LRU
208 rec->fLockCount += 1;
209 }
210 return rec;
reed@google.com602a1d72013-07-23 19:13:54 +0000211}
212
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000213/**
214 This function finds the bounds of the bitmap *within its pixelRef*.
215 If the bitmap lacks a pixelRef, it will return an empty rect, since
216 that doesn't make sense. This may be a useful enough function that
217 it should be somewhere else (in SkBitmap?). */
218static SkIRect get_bounds_from_bitmap(const SkBitmap& bm) {
219 if (!(bm.pixelRef())) {
220 return SkIRect::MakeEmpty();
221 }
222 size_t x, y;
223 SkTDivMod(bm.pixelRefOffset(), bm.rowBytes(), &y, &x);
224 x >>= bm.shiftPerPixel();
225 return SkIRect::MakeXYWH(x, y, bm.width(), bm.height());
226}
227
228
229SkScaledImageCache::ID* SkScaledImageCache::findAndLock(uint32_t genID,
230 int32_t width,
231 int32_t height,
232 SkBitmap* bitmap) {
233 Rec* rec = this->findAndLock(genID, SK_Scalar1, SK_Scalar1,
234 SkIRect::MakeWH(width, height));
235 if (rec) {
236 SkASSERT(NULL == rec->fMip);
237 SkASSERT(rec->fBitmap.pixelRef());
238 *bitmap = rec->fBitmap;
239 }
240 return rec_to_id(rec);
241}
242
reed@google.comd94697c2013-07-24 14:31:33 +0000243SkScaledImageCache::ID* SkScaledImageCache::findAndLock(const SkBitmap& orig,
244 SkScalar scaleX,
245 SkScalar scaleY,
246 SkBitmap* scaled) {
247 if (0 == scaleX || 0 == scaleY) {
248 // degenerate, and the key we use for mipmaps
249 return NULL;
250 }
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000251 Rec* rec = this->findAndLock(orig.getGenerationID(), scaleX,
252 scaleY, get_bounds_from_bitmap(orig));
reed@google.comd94697c2013-07-24 14:31:33 +0000253 if (rec) {
254 SkASSERT(NULL == rec->fMip);
255 SkASSERT(rec->fBitmap.pixelRef());
256 *scaled = rec->fBitmap;
257 }
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000258 return rec_to_id(rec);
reed@google.comd94697c2013-07-24 14:31:33 +0000259}
260
261SkScaledImageCache::ID* SkScaledImageCache::findAndLockMip(const SkBitmap& orig,
262 SkMipMap const ** mip) {
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000263 Rec* rec = this->findAndLock(orig.getGenerationID(), 0, 0,
264 get_bounds_from_bitmap(orig));
reed@google.comd94697c2013-07-24 14:31:33 +0000265 if (rec) {
266 SkASSERT(rec->fMip);
267 SkASSERT(NULL == rec->fBitmap.pixelRef());
268 *mip = rec->fMip;
269 }
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000270 return rec_to_id(rec);
271}
272
273
274////////////////////////////////////////////////////////////////////////////////
275/**
276 This private method is the fully general record adder. All other
277 record adders should call this funtion. */
278void SkScaledImageCache::addAndLock(SkScaledImageCache::Rec* rec) {
279 SkASSERT(rec);
280 this->addToHead(rec);
281 SkASSERT(1 == rec->fLockCount);
282#ifdef USE_HASH
283 SkASSERT(fHash);
284 fHash->add(rec);
285#endif
286 // We may (now) be overbudget, so see if we need to purge something.
287 this->purgeAsNeeded();
288}
289
290SkScaledImageCache::ID* SkScaledImageCache::addAndLock(uint32_t genID,
291 int32_t width,
292 int32_t height,
293 const SkBitmap& bitmap) {
294 Key key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeWH(width, height));
295 Rec* rec = SkNEW_ARGS(Rec, (key, bitmap));
296 this->addAndLock(rec);
297 return rec_to_id(rec);
reed@google.comd94697c2013-07-24 14:31:33 +0000298}
299
reed@google.com602a1d72013-07-23 19:13:54 +0000300SkScaledImageCache::ID* SkScaledImageCache::addAndLock(const SkBitmap& orig,
301 SkScalar scaleX,
302 SkScalar scaleY,
303 const SkBitmap& scaled) {
reed@google.comd94697c2013-07-24 14:31:33 +0000304 if (0 == scaleX || 0 == scaleY) {
305 // degenerate, and the key we use for mipmaps
306 return NULL;
307 }
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000308 SkIRect bounds = get_bounds_from_bitmap(orig);
309 if (bounds.isEmpty()) {
reed@google.com602a1d72013-07-23 19:13:54 +0000310 return NULL;
311 }
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000312 Key key(orig.getGenerationID(), scaleX, scaleY, bounds);
reed@google.com602a1d72013-07-23 19:13:54 +0000313 Rec* rec = SkNEW_ARGS(Rec, (key, scaled));
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000314 this->addAndLock(rec);
315 return rec_to_id(rec);
reed@google.comd94697c2013-07-24 14:31:33 +0000316}
reed@google.com602a1d72013-07-23 19:13:54 +0000317
reed@google.comd94697c2013-07-24 14:31:33 +0000318SkScaledImageCache::ID* SkScaledImageCache::addAndLockMip(const SkBitmap& orig,
319 const SkMipMap* mip) {
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000320 SkIRect bounds = get_bounds_from_bitmap(orig);
321 if (bounds.isEmpty()) {
reed@google.comd94697c2013-07-24 14:31:33 +0000322 return NULL;
323 }
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000324 Key key(orig.getGenerationID(), 0, 0, bounds);
reed@google.comd94697c2013-07-24 14:31:33 +0000325 Rec* rec = SkNEW_ARGS(Rec, (key, mip));
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000326 this->addAndLock(rec);
327 return rec_to_id(rec);
reed@google.com602a1d72013-07-23 19:13:54 +0000328}
329
330void SkScaledImageCache::unlock(SkScaledImageCache::ID* id) {
331 SkASSERT(id);
332
333#ifdef SK_DEBUG
334 {
335 bool found = false;
336 Rec* rec = fHead;
337 while (rec != NULL) {
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000338 if (rec == id_to_rec(id)) {
reed@google.com602a1d72013-07-23 19:13:54 +0000339 found = true;
340 break;
341 }
342 rec = rec->fNext;
343 }
344 SkASSERT(found);
345 }
346#endif
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000347 Rec* rec = id_to_rec(id);
reed@google.com602a1d72013-07-23 19:13:54 +0000348 SkASSERT(rec->fLockCount > 0);
349 rec->fLockCount -= 1;
350
reed@google.com602a1d72013-07-23 19:13:54 +0000351 // we may have been over-budget, but now have released something, so check
352 // if we should purge.
353 if (0 == rec->fLockCount) {
354 this->purgeAsNeeded();
355 }
356}
357
358void SkScaledImageCache::purgeAsNeeded() {
359 size_t byteLimit = fByteLimit;
360 size_t bytesUsed = fBytesUsed;
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +0000361
reed@google.com602a1d72013-07-23 19:13:54 +0000362 Rec* rec = fTail;
363 while (rec) {
364 if (bytesUsed < byteLimit) {
365 break;
366 }
367 Rec* prev = rec->fPrev;
368 if (0 == rec->fLockCount) {
reed@google.com602a1d72013-07-23 19:13:54 +0000369 size_t used = rec->bytesUsed();
370 SkASSERT(used <= bytesUsed);
371 bytesUsed -= used;
372 this->detach(rec);
reed@google.com5d1e5582013-07-25 14:36:15 +0000373#ifdef USE_HASH
374 fHash->remove(rec->fKey);
375#endif
skia.committer@gmail.com956b3102013-07-26 07:00:58 +0000376
reed@google.com602a1d72013-07-23 19:13:54 +0000377 SkDELETE(rec);
378 fCount -= 1;
379 }
380 rec = prev;
381 }
382 fBytesUsed = bytesUsed;
383}
384
385size_t SkScaledImageCache::setByteLimit(size_t newLimit) {
386 size_t prevLimit = fByteLimit;
387 fByteLimit = newLimit;
388 if (newLimit < prevLimit) {
389 this->purgeAsNeeded();
390 }
391 return prevLimit;
392}
393
394///////////////////////////////////////////////////////////////////////////////
395
396void SkScaledImageCache::detach(Rec* rec) {
397 Rec* prev = rec->fPrev;
398 Rec* next = rec->fNext;
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +0000399
reed@google.com602a1d72013-07-23 19:13:54 +0000400 if (!prev) {
401 SkASSERT(fHead == rec);
402 fHead = next;
403 } else {
404 prev->fNext = next;
405 }
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +0000406
reed@google.com602a1d72013-07-23 19:13:54 +0000407 if (!next) {
408 fTail = prev;
409 } else {
410 next->fPrev = prev;
411 }
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +0000412
reed@google.com602a1d72013-07-23 19:13:54 +0000413 rec->fNext = rec->fPrev = NULL;
414}
415
416void SkScaledImageCache::moveToHead(Rec* rec) {
417 if (fHead == rec) {
418 return;
419 }
420
421 SkASSERT(fHead);
422 SkASSERT(fTail);
423
424 this->validate();
425
426 this->detach(rec);
427
428 fHead->fPrev = rec;
429 rec->fNext = fHead;
430 fHead = rec;
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +0000431
reed@google.com602a1d72013-07-23 19:13:54 +0000432 this->validate();
433}
434
435void SkScaledImageCache::addToHead(Rec* rec) {
436 this->validate();
437
438 rec->fPrev = NULL;
439 rec->fNext = fHead;
440 if (fHead) {
441 fHead->fPrev = rec;
442 }
443 fHead = rec;
444 if (!fTail) {
445 fTail = rec;
446 }
447 fBytesUsed += rec->bytesUsed();
448 fCount += 1;
449
450 this->validate();
451}
452
453#ifdef SK_DEBUG
454void SkScaledImageCache::validate() const {
455 if (NULL == fHead) {
456 SkASSERT(NULL == fTail);
457 SkASSERT(0 == fBytesUsed);
458 return;
459 }
460
461 if (fHead == fTail) {
462 SkASSERT(NULL == fHead->fPrev);
463 SkASSERT(NULL == fHead->fNext);
464 SkASSERT(fHead->bytesUsed() == fBytesUsed);
465 return;
466 }
467
468 SkASSERT(NULL == fHead->fPrev);
469 SkASSERT(NULL != fHead->fNext);
470 SkASSERT(NULL == fTail->fNext);
471 SkASSERT(NULL != fTail->fPrev);
472
473 size_t used = 0;
474 int count = 0;
475 const Rec* rec = fHead;
476 while (rec) {
477 count += 1;
478 used += rec->bytesUsed();
479 SkASSERT(used <= fBytesUsed);
480 rec = rec->fNext;
481 }
482 SkASSERT(fCount == count);
483
484 rec = fTail;
485 while (rec) {
486 SkASSERT(count > 0);
487 count -= 1;
488 SkASSERT(used >= rec->bytesUsed());
489 used -= rec->bytesUsed();
490 rec = rec->fPrev;
491 }
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +0000492
reed@google.com602a1d72013-07-23 19:13:54 +0000493 SkASSERT(0 == count);
494 SkASSERT(0 == used);
495}
496#endif
497
498///////////////////////////////////////////////////////////////////////////////
499
500#include "SkThread.h"
501
reed@google.combe19dbe2013-07-24 15:06:34 +0000502SK_DECLARE_STATIC_MUTEX(gMutex);
reed@google.com602a1d72013-07-23 19:13:54 +0000503
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000504static void create_cache(SkScaledImageCache** cache) {
505 *cache = SkNEW_ARGS(SkScaledImageCache, (SK_DEFAULT_IMAGE_CACHE_LIMIT));
506}
507
reed@google.com602a1d72013-07-23 19:13:54 +0000508static SkScaledImageCache* get_cache() {
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000509 static SkScaledImageCache* gCache(NULL);
510 SK_DECLARE_STATIC_ONCE(create_cache_once);
511 SkOnce<SkScaledImageCache**>(&create_cache_once, create_cache, &gCache);
512 SkASSERT(NULL != gCache);
reed@google.com602a1d72013-07-23 19:13:54 +0000513 return gCache;
514}
515
commit-bot@chromium.org75854792013-10-29 19:55:00 +0000516
517SkScaledImageCache::ID* SkScaledImageCache::FindAndLock(
518 uint32_t pixelGenerationID,
519 int32_t width,
520 int32_t height,
521 SkBitmap* scaled) {
522 SkAutoMutexAcquire am(gMutex);
523 return get_cache()->findAndLock(pixelGenerationID, width, height, scaled);
524}
525
526SkScaledImageCache::ID* SkScaledImageCache::AddAndLock(
527 uint32_t pixelGenerationID,
528 int32_t width,
529 int32_t height,
530 const SkBitmap& scaled) {
531 SkAutoMutexAcquire am(gMutex);
532 return get_cache()->addAndLock(pixelGenerationID, width, height, scaled);
533}
534
535
reed@google.com602a1d72013-07-23 19:13:54 +0000536SkScaledImageCache::ID* SkScaledImageCache::FindAndLock(const SkBitmap& orig,
537 SkScalar scaleX,
538 SkScalar scaleY,
539 SkBitmap* scaled) {
540 SkAutoMutexAcquire am(gMutex);
541 return get_cache()->findAndLock(orig, scaleX, scaleY, scaled);
542}
543
reed@google.comd94697c2013-07-24 14:31:33 +0000544SkScaledImageCache::ID* SkScaledImageCache::FindAndLockMip(const SkBitmap& orig,
545 SkMipMap const ** mip) {
546 SkAutoMutexAcquire am(gMutex);
547 return get_cache()->findAndLockMip(orig, mip);
548}
549
reed@google.com602a1d72013-07-23 19:13:54 +0000550SkScaledImageCache::ID* SkScaledImageCache::AddAndLock(const SkBitmap& orig,
551 SkScalar scaleX,
552 SkScalar scaleY,
553 const SkBitmap& scaled) {
554 SkAutoMutexAcquire am(gMutex);
555 return get_cache()->addAndLock(orig, scaleX, scaleY, scaled);
556}
557
reed@google.comd94697c2013-07-24 14:31:33 +0000558SkScaledImageCache::ID* SkScaledImageCache::AddAndLockMip(const SkBitmap& orig,
559 const SkMipMap* mip) {
560 SkAutoMutexAcquire am(gMutex);
561 return get_cache()->addAndLockMip(orig, mip);
562}
563
reed@google.com602a1d72013-07-23 19:13:54 +0000564void SkScaledImageCache::Unlock(SkScaledImageCache::ID* id) {
565 SkAutoMutexAcquire am(gMutex);
566 return get_cache()->unlock(id);
567}
568
569size_t SkScaledImageCache::GetBytesUsed() {
570 SkAutoMutexAcquire am(gMutex);
571 return get_cache()->getBytesUsed();
572}
573
574size_t SkScaledImageCache::GetByteLimit() {
575 SkAutoMutexAcquire am(gMutex);
576 return get_cache()->getByteLimit();
577}
578
579size_t SkScaledImageCache::SetByteLimit(size_t newLimit) {
580 SkAutoMutexAcquire am(gMutex);
581 return get_cache()->setByteLimit(newLimit);
582}
583
584///////////////////////////////////////////////////////////////////////////////
585
586#include "SkGraphics.h"
587
588size_t SkGraphics::GetImageCacheBytesUsed() {
589 return SkScaledImageCache::GetBytesUsed();
590}
591
592size_t SkGraphics::GetImageCacheByteLimit() {
593 return SkScaledImageCache::GetByteLimit();
594}
595
596size_t SkGraphics::SetImageCacheByteLimit(size_t newLimit) {
597 return SkScaledImageCache::SetByteLimit(newLimit);
598}