Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(404)

Side by Side Diff: src/core/SkScaledImageCache.cpp

Issue 483493003: expose generalized imagecache key (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 261 matching lines...) Expand 10 before | Expand all | Expand 10 after
272 272
273 uint32_t fGenID; 273 uint32_t fGenID;
274 SkScalar fScaleX; 274 SkScalar fScaleX;
275 SkScalar fScaleY; 275 SkScalar fScaleY;
276 SkIRect fBounds; 276 SkIRect fBounds;
277 }; 277 };
278 278
279 SkScaledImageCache::Rec* SkScaledImageCache::findAndLock(uint32_t genID, 279 SkScaledImageCache::Rec* SkScaledImageCache::findAndLock(uint32_t genID,
280 SkScalar scaleX, 280 SkScalar scaleX,
281 SkScalar scaleY, 281 SkScalar scaleY,
282 const SkIRect& bounds) { 282 const SkIRect& bounds) {
qiankun 2014/08/21 16:15:03 This function doesn't need any more.
reed1 2014/08/21 16:32:46 Good catch. Done.
283 return this->findAndLock(GenWHKey(genID, scaleX, scaleY, bounds)); 283 return this->findAndLock(GenWHKey(genID, scaleX, scaleY, bounds));
284 } 284 }
285 285
286 /** 286 /**
287 This private method is the fully general record finder. All other 287 This private method is the fully general record finder. All other
288 record finders should call this function or the one above. */ 288 record finders should call this function or the one above. */
289 SkScaledImageCache::Rec* SkScaledImageCache::findAndLock(const SkScaledImageCach e::Key& key) { 289 SkScaledImageCache::Rec* SkScaledImageCache::findAndLock(const SkScaledImageCach e::Key& key) {
290 #ifdef USE_HASH 290 #ifdef USE_HASH
291 Rec* rec = fHash->find(key); 291 Rec* rec = fHash->find(key);
292 #else 292 #else
293 Rec* rec = find_rec_in_list(fHead, key); 293 Rec* rec = find_rec_in_list(fHead, key);
294 #endif 294 #endif
295 if (rec) { 295 if (rec) {
296 this->moveToHead(rec); // for our LRU 296 this->moveToHead(rec); // for our LRU
297 rec->fLockCount += 1; 297 rec->fLockCount += 1;
298 } 298 }
299 return rec; 299 return rec;
300 } 300 }
301 301
302 /** 302 SkScaledImageCache::ID* SkScaledImageCache::findAndLock(const Key& key, SkBitmap * result) {
303 This function finds the bounds of the bitmap *within its pixelRef*. 303 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) { 304 if (rec) {
323 SkASSERT(NULL == rec->fMip); 305 SkASSERT(NULL == rec->fMip);
324 SkASSERT(rec->fBitmap.pixelRef()); 306 SkASSERT(rec->fBitmap.pixelRef());
325 *bitmap = rec->fBitmap; 307 *result = rec->fBitmap;
326 } 308 }
327 return rec_to_id(rec); 309 return rec_to_id(rec);
328 } 310 }
329 311
330 SkScaledImageCache::ID* SkScaledImageCache::findAndLock(const SkBitmap& orig, 312 SkScaledImageCache::ID* SkScaledImageCache::findAndLock(const Key& key, SkMipMap const ** mip) {
331 SkScalar scaleX, 313 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) { 314 if (rec) {
353 SkASSERT(rec->fMip); 315 SkASSERT(rec->fMip);
354 SkASSERT(NULL == rec->fBitmap.pixelRef()); 316 SkASSERT(NULL == rec->fBitmap.pixelRef());
355 *mip = rec->fMip; 317 *mip = rec->fMip;
356 } 318 }
357 return rec_to_id(rec); 319 return rec_to_id(rec);
358 } 320 }
359 321
360 322
361 //////////////////////////////////////////////////////////////////////////////// 323 ////////////////////////////////////////////////////////////////////////////////
(...skipping 16 matching lines...) Expand all
378 SkASSERT(1 == rec->fLockCount); 340 SkASSERT(1 == rec->fLockCount);
379 #ifdef USE_HASH 341 #ifdef USE_HASH
380 SkASSERT(fHash); 342 SkASSERT(fHash);
381 fHash->add(rec); 343 fHash->add(rec);
382 #endif 344 #endif
383 // We may (now) be overbudget, so see if we need to purge something. 345 // We may (now) be overbudget, so see if we need to purge something.
384 this->purgeAsNeeded(); 346 this->purgeAsNeeded();
385 return rec_to_id(rec); 347 return rec_to_id(rec);
386 } 348 }
387 349
388 SkScaledImageCache::ID* SkScaledImageCache::addAndLock(uint32_t genID, 350 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)); 351 Rec* rec = SkNEW_ARGS(Rec, (key, scaled));
411 return this->addAndLock(rec); 352 return this->addAndLock(rec);
412 } 353 }
413 354
414 SkScaledImageCache::ID* SkScaledImageCache::addAndLockMip(const SkBitmap& orig, 355 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)); 356 Rec* rec = SkNEW_ARGS(Rec, (key, mip));
422 return this->addAndLock(rec); 357 return this->addAndLock(rec);
423 } 358 }
424 359
425 void SkScaledImageCache::unlock(SkScaledImageCache::ID* id) { 360 void SkScaledImageCache::unlock(SkScaledImageCache::ID* id) {
426 SkASSERT(id); 361 SkASSERT(id);
427 362
428 #ifdef SK_DEBUG 363 #ifdef SK_DEBUG
429 { 364 {
430 bool found = false; 365 bool found = false;
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
656 #ifdef SK_USE_DISCARDABLE_SCALEDIMAGECACHE 591 #ifdef SK_USE_DISCARDABLE_SCALEDIMAGECACHE
657 gScaledImageCache = SkNEW_ARGS(SkScaledImageCache, (SkDiscardableMemory: :Create)); 592 gScaledImageCache = SkNEW_ARGS(SkScaledImageCache, (SkDiscardableMemory: :Create));
658 #else 593 #else
659 gScaledImageCache = SkNEW_ARGS(SkScaledImageCache, (SK_DEFAULT_IMAGE_CAC HE_LIMIT)); 594 gScaledImageCache = SkNEW_ARGS(SkScaledImageCache, (SK_DEFAULT_IMAGE_CAC HE_LIMIT));
660 #endif 595 #endif
661 atexit(cleanup_gScaledImageCache); 596 atexit(cleanup_gScaledImageCache);
662 } 597 }
663 return gScaledImageCache; 598 return gScaledImageCache;
664 } 599 }
665 600
666 601 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); 602 SkAutoMutexAcquire am(gMutex);
673 return get_cache()->findAndLock(pixelGenerationID, width, height, scaled); 603 return get_cache()->findAndLock(key, result);
674 } 604 }
675 605
676 SkScaledImageCache::ID* SkScaledImageCache::AddAndLock( 606 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); 607 SkAutoMutexAcquire am(gMutex);
682 return get_cache()->addAndLock(pixelGenerationID, width, height, scaled); 608 return get_cache()->findAndLock(key, mip);
683 } 609 }
684 610
685 611 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); 612 SkAutoMutexAcquire am(gMutex);
691 return get_cache()->findAndLock(orig, scaleX, scaleY, scaled); 613 return get_cache()->addAndLock(key, scaled);
692 } 614 }
693 615
694 SkScaledImageCache::ID* SkScaledImageCache::FindAndLockMip(const SkBitmap& orig, 616 SkScaledImageCache::ID* SkScaledImageCache::AddAndLock(const Key& key, const SkM ipMap* mip) {
695 SkMipMap const ** mip) {
696 SkAutoMutexAcquire am(gMutex); 617 SkAutoMutexAcquire am(gMutex);
697 return get_cache()->findAndLockMip(orig, mip); 618 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 } 619 }
713 620
714 void SkScaledImageCache::Unlock(SkScaledImageCache::ID* id) { 621 void SkScaledImageCache::Unlock(SkScaledImageCache::ID* id) {
715 SkAutoMutexAcquire am(gMutex); 622 SkAutoMutexAcquire am(gMutex);
716 get_cache()->unlock(id); 623 get_cache()->unlock(id);
717 624
718 // get_cache()->dump(); 625 // get_cache()->dump();
719 } 626 }
720 627
721 size_t SkScaledImageCache::GetTotalBytesUsed() { 628 size_t SkScaledImageCache::GetTotalBytesUsed() {
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
770 } 677 }
771 678
772 size_t SkGraphics::GetImageCacheSingleAllocationByteLimit() { 679 size_t SkGraphics::GetImageCacheSingleAllocationByteLimit() {
773 return SkScaledImageCache::GetSingleAllocationByteLimit(); 680 return SkScaledImageCache::GetSingleAllocationByteLimit();
774 } 681 }
775 682
776 size_t SkGraphics::SetImageCacheSingleAllocationByteLimit(size_t newLimit) { 683 size_t SkGraphics::SetImageCacheSingleAllocationByteLimit(size_t newLimit) {
777 return SkScaledImageCache::SetSingleAllocationByteLimit(newLimit); 684 return SkScaledImageCache::SetSingleAllocationByteLimit(newLimit);
778 } 685 }
779 686
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698