OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 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 "SkChecksum.h" | 8 #include "SkChecksum.h" |
9 #include "SkScaledImageCache.h" | 9 #include "SkScaledImageCache.h" |
10 #include "SkMipMap.h" | 10 #include "SkMipMap.h" |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 while (rec) { | 253 while (rec) { |
254 Rec* next = rec->fNext; | 254 Rec* next = rec->fNext; |
255 SkDELETE(rec); | 255 SkDELETE(rec); |
256 rec = next; | 256 rec = next; |
257 } | 257 } |
258 delete fHash; | 258 delete fHash; |
259 } | 259 } |
260 | 260 |
261 //////////////////////////////////////////////////////////////////////////////// | 261 //////////////////////////////////////////////////////////////////////////////// |
262 | 262 |
263 struct GenWHKey : public SkScaledImageCache::Key { | |
264 public: | |
265 GenWHKey(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& bo
unds) | |
266 : fGenID(genID) | |
267 , fScaleX(scaleX) | |
268 , fScaleY(scaleY) | |
269 , fBounds(bounds) { | |
270 this->init(7 * sizeof(uint32_t)); | |
271 } | |
272 | |
273 uint32_t fGenID; | |
274 SkScalar fScaleX; | |
275 SkScalar fScaleY; | |
276 SkIRect fBounds; | |
277 }; | |
278 | |
279 SkScaledImageCache::Rec* SkScaledImageCache::findAndLock(uint32_t genID, | |
280 SkScalar scaleX, | |
281 SkScalar scaleY, | |
282 const SkIRect& bounds) { | |
283 return this->findAndLock(GenWHKey(genID, scaleX, scaleY, bounds)); | |
284 } | |
285 | |
286 /** | 263 /** |
287 This private method is the fully general record finder. All other | 264 This private method is the fully general record finder. All other |
288 record finders should call this function or the one above. */ | 265 record finders should call this function or the one above. |
| 266 */ |
289 SkScaledImageCache::Rec* SkScaledImageCache::findAndLock(const SkScaledImageCach
e::Key& key) { | 267 SkScaledImageCache::Rec* SkScaledImageCache::findAndLock(const SkScaledImageCach
e::Key& key) { |
290 #ifdef USE_HASH | 268 #ifdef USE_HASH |
291 Rec* rec = fHash->find(key); | 269 Rec* rec = fHash->find(key); |
292 #else | 270 #else |
293 Rec* rec = find_rec_in_list(fHead, key); | 271 Rec* rec = find_rec_in_list(fHead, key); |
294 #endif | 272 #endif |
295 if (rec) { | 273 if (rec) { |
296 this->moveToHead(rec); // for our LRU | 274 this->moveToHead(rec); // for our LRU |
297 rec->fLockCount += 1; | 275 rec->fLockCount += 1; |
298 } | 276 } |
299 return rec; | 277 return rec; |
300 } | 278 } |
301 | 279 |
302 /** | 280 SkScaledImageCache::ID* SkScaledImageCache::findAndLock(const Key& key, SkBitmap
* result) { |
303 This function finds the bounds of the bitmap *within its pixelRef*. | 281 Rec* rec = this->findAndLock(key); |
304 If the bitmap lacks a pixelRef, it will return an empty rect, since | |
305 that doesn't make sense. This may be a useful enough function that | |
306 it should be somewhere else (in SkBitmap?). */ | |
307 static SkIRect get_bounds_from_bitmap(const SkBitmap& bm) { | |
308 if (!(bm.pixelRef())) { | |
309 return SkIRect::MakeEmpty(); | |
310 } | |
311 SkIPoint origin = bm.pixelRefOrigin(); | |
312 return SkIRect::MakeXYWH(origin.fX, origin.fY, bm.width(), bm.height()); | |
313 } | |
314 | |
315 | |
316 SkScaledImageCache::ID* SkScaledImageCache::findAndLock(uint32_t genID, | |
317 int32_t width, | |
318 int32_t height, | |
319 SkBitmap* bitmap) { | |
320 Rec* rec = this->findAndLock(genID, SK_Scalar1, SK_Scalar1, | |
321 SkIRect::MakeWH(width, height)); | |
322 if (rec) { | 282 if (rec) { |
323 SkASSERT(NULL == rec->fMip); | 283 SkASSERT(NULL == rec->fMip); |
324 SkASSERT(rec->fBitmap.pixelRef()); | 284 SkASSERT(rec->fBitmap.pixelRef()); |
325 *bitmap = rec->fBitmap; | 285 *result = rec->fBitmap; |
326 } | 286 } |
327 return rec_to_id(rec); | 287 return rec_to_id(rec); |
328 } | 288 } |
329 | 289 |
330 SkScaledImageCache::ID* SkScaledImageCache::findAndLock(const SkBitmap& orig, | 290 SkScaledImageCache::ID* SkScaledImageCache::findAndLock(const Key& key, const Sk
MipMap** mip) { |
331 SkScalar scaleX, | 291 Rec* rec = this->findAndLock(key); |
332 SkScalar scaleY, | |
333 SkBitmap* scaled) { | |
334 if (0 == scaleX || 0 == scaleY) { | |
335 // degenerate, and the key we use for mipmaps | |
336 return NULL; | |
337 } | |
338 Rec* rec = this->findAndLock(orig.getGenerationID(), scaleX, | |
339 scaleY, get_bounds_from_bitmap(orig)); | |
340 if (rec) { | |
341 SkASSERT(NULL == rec->fMip); | |
342 SkASSERT(rec->fBitmap.pixelRef()); | |
343 *scaled = rec->fBitmap; | |
344 } | |
345 return rec_to_id(rec); | |
346 } | |
347 | |
348 SkScaledImageCache::ID* SkScaledImageCache::findAndLockMip(const SkBitmap& orig, | |
349 SkMipMap const ** mip
) { | |
350 Rec* rec = this->findAndLock(orig.getGenerationID(), 0, 0, | |
351 get_bounds_from_bitmap(orig)); | |
352 if (rec) { | 292 if (rec) { |
353 SkASSERT(rec->fMip); | 293 SkASSERT(rec->fMip); |
354 SkASSERT(NULL == rec->fBitmap.pixelRef()); | 294 SkASSERT(NULL == rec->fBitmap.pixelRef()); |
355 *mip = rec->fMip; | 295 *mip = rec->fMip; |
356 } | 296 } |
357 return rec_to_id(rec); | 297 return rec_to_id(rec); |
358 } | 298 } |
359 | 299 |
360 | 300 |
361 //////////////////////////////////////////////////////////////////////////////// | 301 //////////////////////////////////////////////////////////////////////////////// |
(...skipping 16 matching lines...) Expand all Loading... |
378 SkASSERT(1 == rec->fLockCount); | 318 SkASSERT(1 == rec->fLockCount); |
379 #ifdef USE_HASH | 319 #ifdef USE_HASH |
380 SkASSERT(fHash); | 320 SkASSERT(fHash); |
381 fHash->add(rec); | 321 fHash->add(rec); |
382 #endif | 322 #endif |
383 // We may (now) be overbudget, so see if we need to purge something. | 323 // We may (now) be overbudget, so see if we need to purge something. |
384 this->purgeAsNeeded(); | 324 this->purgeAsNeeded(); |
385 return rec_to_id(rec); | 325 return rec_to_id(rec); |
386 } | 326 } |
387 | 327 |
388 SkScaledImageCache::ID* SkScaledImageCache::addAndLock(uint32_t genID, | 328 SkScaledImageCache::ID* SkScaledImageCache::addAndLock(const Key& key, const SkB
itmap& scaled) { |
389 int32_t width, | |
390 int32_t height, | |
391 const SkBitmap& bitmap) { | |
392 GenWHKey key(genID, SK_Scalar1, SK_Scalar1, SkIRect::MakeWH(width, height)); | |
393 Rec* rec = SkNEW_ARGS(Rec, (key, bitmap)); | |
394 return this->addAndLock(rec); | |
395 } | |
396 | |
397 SkScaledImageCache::ID* SkScaledImageCache::addAndLock(const SkBitmap& orig, | |
398 SkScalar scaleX, | |
399 SkScalar scaleY, | |
400 const SkBitmap& scaled) { | |
401 if (0 == scaleX || 0 == scaleY) { | |
402 // degenerate, and the key we use for mipmaps | |
403 return NULL; | |
404 } | |
405 SkIRect bounds = get_bounds_from_bitmap(orig); | |
406 if (bounds.isEmpty()) { | |
407 return NULL; | |
408 } | |
409 GenWHKey key(orig.getGenerationID(), scaleX, scaleY, bounds); | |
410 Rec* rec = SkNEW_ARGS(Rec, (key, scaled)); | 329 Rec* rec = SkNEW_ARGS(Rec, (key, scaled)); |
411 return this->addAndLock(rec); | 330 return this->addAndLock(rec); |
412 } | 331 } |
413 | 332 |
414 SkScaledImageCache::ID* SkScaledImageCache::addAndLockMip(const SkBitmap& orig, | 333 SkScaledImageCache::ID* SkScaledImageCache::addAndLock(const Key& key, const SkM
ipMap* mip) { |
415 const SkMipMap* mip) { | |
416 SkIRect bounds = get_bounds_from_bitmap(orig); | |
417 if (bounds.isEmpty()) { | |
418 return NULL; | |
419 } | |
420 GenWHKey key(orig.getGenerationID(), 0, 0, bounds); | |
421 Rec* rec = SkNEW_ARGS(Rec, (key, mip)); | 334 Rec* rec = SkNEW_ARGS(Rec, (key, mip)); |
422 return this->addAndLock(rec); | 335 return this->addAndLock(rec); |
423 } | 336 } |
424 | 337 |
425 void SkScaledImageCache::unlock(SkScaledImageCache::ID* id) { | 338 void SkScaledImageCache::unlock(SkScaledImageCache::ID* id) { |
426 SkASSERT(id); | 339 SkASSERT(id); |
427 | 340 |
428 #ifdef SK_DEBUG | 341 #ifdef SK_DEBUG |
429 { | 342 { |
430 bool found = false; | 343 bool found = false; |
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
656 #ifdef SK_USE_DISCARDABLE_SCALEDIMAGECACHE | 569 #ifdef SK_USE_DISCARDABLE_SCALEDIMAGECACHE |
657 gScaledImageCache = SkNEW_ARGS(SkScaledImageCache, (SkDiscardableMemory:
:Create)); | 570 gScaledImageCache = SkNEW_ARGS(SkScaledImageCache, (SkDiscardableMemory:
:Create)); |
658 #else | 571 #else |
659 gScaledImageCache = SkNEW_ARGS(SkScaledImageCache, (SK_DEFAULT_IMAGE_CAC
HE_LIMIT)); | 572 gScaledImageCache = SkNEW_ARGS(SkScaledImageCache, (SK_DEFAULT_IMAGE_CAC
HE_LIMIT)); |
660 #endif | 573 #endif |
661 atexit(cleanup_gScaledImageCache); | 574 atexit(cleanup_gScaledImageCache); |
662 } | 575 } |
663 return gScaledImageCache; | 576 return gScaledImageCache; |
664 } | 577 } |
665 | 578 |
666 | 579 SkScaledImageCache::ID* SkScaledImageCache::FindAndLock(const Key& key, SkBitmap
* result) { |
667 SkScaledImageCache::ID* SkScaledImageCache::FindAndLock( | |
668 uint32_t pixelGenerationID, | |
669 int32_t width, | |
670 int32_t height, | |
671 SkBitmap* scaled) { | |
672 SkAutoMutexAcquire am(gMutex); | 580 SkAutoMutexAcquire am(gMutex); |
673 return get_cache()->findAndLock(pixelGenerationID, width, height, scaled); | 581 return get_cache()->findAndLock(key, result); |
674 } | 582 } |
675 | 583 |
676 SkScaledImageCache::ID* SkScaledImageCache::AddAndLock( | 584 SkScaledImageCache::ID* SkScaledImageCache::FindAndLock(const Key& key, SkMipMap
const ** mip) { |
677 uint32_t pixelGenerationID, | |
678 int32_t width, | |
679 int32_t height, | |
680 const SkBitmap& scaled) { | |
681 SkAutoMutexAcquire am(gMutex); | 585 SkAutoMutexAcquire am(gMutex); |
682 return get_cache()->addAndLock(pixelGenerationID, width, height, scaled); | 586 return get_cache()->findAndLock(key, mip); |
683 } | 587 } |
684 | 588 |
685 | 589 SkScaledImageCache::ID* SkScaledImageCache::AddAndLock(const Key& key, const SkB
itmap& scaled) { |
686 SkScaledImageCache::ID* SkScaledImageCache::FindAndLock(const SkBitmap& orig, | |
687 SkScalar scaleX, | |
688 SkScalar scaleY, | |
689 SkBitmap* scaled) { | |
690 SkAutoMutexAcquire am(gMutex); | 590 SkAutoMutexAcquire am(gMutex); |
691 return get_cache()->findAndLock(orig, scaleX, scaleY, scaled); | 591 return get_cache()->addAndLock(key, scaled); |
692 } | 592 } |
693 | 593 |
694 SkScaledImageCache::ID* SkScaledImageCache::FindAndLockMip(const SkBitmap& orig, | 594 SkScaledImageCache::ID* SkScaledImageCache::AddAndLock(const Key& key, const SkM
ipMap* mip) { |
695 SkMipMap const ** mip) { | |
696 SkAutoMutexAcquire am(gMutex); | 595 SkAutoMutexAcquire am(gMutex); |
697 return get_cache()->findAndLockMip(orig, mip); | 596 return get_cache()->addAndLock(key, mip); |
698 } | |
699 | |
700 SkScaledImageCache::ID* SkScaledImageCache::AddAndLock(const SkBitmap& orig, | |
701 SkScalar scaleX, | |
702 SkScalar scaleY, | |
703 const SkBitmap& scaled) { | |
704 SkAutoMutexAcquire am(gMutex); | |
705 return get_cache()->addAndLock(orig, scaleX, scaleY, scaled); | |
706 } | |
707 | |
708 SkScaledImageCache::ID* SkScaledImageCache::AddAndLockMip(const SkBitmap& orig, | |
709 const SkMipMap* mip) { | |
710 SkAutoMutexAcquire am(gMutex); | |
711 return get_cache()->addAndLockMip(orig, mip); | |
712 } | 597 } |
713 | 598 |
714 void SkScaledImageCache::Unlock(SkScaledImageCache::ID* id) { | 599 void SkScaledImageCache::Unlock(SkScaledImageCache::ID* id) { |
715 SkAutoMutexAcquire am(gMutex); | 600 SkAutoMutexAcquire am(gMutex); |
716 get_cache()->unlock(id); | 601 get_cache()->unlock(id); |
717 | 602 |
718 // get_cache()->dump(); | 603 // get_cache()->dump(); |
719 } | 604 } |
720 | 605 |
721 size_t SkScaledImageCache::GetTotalBytesUsed() { | 606 size_t SkScaledImageCache::GetTotalBytesUsed() { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
770 } | 655 } |
771 | 656 |
772 size_t SkGraphics::GetImageCacheSingleAllocationByteLimit() { | 657 size_t SkGraphics::GetImageCacheSingleAllocationByteLimit() { |
773 return SkScaledImageCache::GetSingleAllocationByteLimit(); | 658 return SkScaledImageCache::GetSingleAllocationByteLimit(); |
774 } | 659 } |
775 | 660 |
776 size_t SkGraphics::SetImageCacheSingleAllocationByteLimit(size_t newLimit) { | 661 size_t SkGraphics::SetImageCacheSingleAllocationByteLimit(size_t newLimit) { |
777 return SkScaledImageCache::SetSingleAllocationByteLimit(newLimit); | 662 return SkScaledImageCache::SetSingleAllocationByteLimit(newLimit); |
778 } | 663 } |
779 | 664 |
OLD | NEW |