| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ash/app_list/icon_cache.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/md5.h" | |
| 9 #include "ui/gfx/size.h" | |
| 10 | |
| 11 namespace { | |
| 12 | |
| 13 // Gets cache key based on |image| contents and desired |size|. | |
| 14 std::string GetKey(const SkBitmap& image, const gfx::Size& size) { | |
| 15 SkAutoLockPixels image_lock(image); | |
| 16 base::MD5Digest digest; | |
| 17 MD5Sum(image.getPixels(), image.getSize(), &digest); | |
| 18 | |
| 19 return MD5DigestToBase16(digest) + "." + size.ToString(); | |
| 20 } | |
| 21 | |
| 22 } // namespace | |
| 23 | |
| 24 namespace ash { | |
| 25 | |
| 26 // static | |
| 27 IconCache* IconCache::instance_ = NULL; | |
| 28 | |
| 29 // static | |
| 30 void IconCache::CreateInstance() { | |
| 31 DCHECK(!instance_); | |
| 32 instance_ = new IconCache; | |
| 33 } | |
| 34 | |
| 35 // static | |
| 36 void IconCache::DeleteInstance() { | |
| 37 DCHECK(instance_); | |
| 38 delete instance_; | |
| 39 instance_ = NULL; | |
| 40 } | |
| 41 | |
| 42 // static | |
| 43 IconCache* IconCache::GetInstance() { | |
| 44 DCHECK(instance_); | |
| 45 return instance_; | |
| 46 } | |
| 47 | |
| 48 void IconCache::MarkAllEntryUnused() { | |
| 49 for (Cache::iterator i = cache_.begin(); i != cache_.end(); ++i) | |
| 50 i->second.used = false; | |
| 51 } | |
| 52 | |
| 53 void IconCache::PurgeAllUnused() { | |
| 54 for (Cache::iterator i = cache_.begin(); i != cache_.end();) { | |
| 55 Cache::iterator current(i); | |
| 56 ++i; | |
| 57 if (!current->second.used) | |
| 58 cache_.erase(current); | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 bool IconCache::Get(const SkBitmap& src, | |
| 63 const gfx::Size& size, | |
| 64 SkBitmap* processed) { | |
| 65 Cache::iterator it = cache_.find(GetKey(src, size)); | |
| 66 if (it == cache_.end()) | |
| 67 return false; | |
| 68 | |
| 69 it->second.used = true; | |
| 70 | |
| 71 if (processed) | |
| 72 *processed = it->second.image; | |
| 73 return true; | |
| 74 } | |
| 75 | |
| 76 void IconCache::Put(const SkBitmap& src, | |
| 77 const gfx::Size& size, | |
| 78 const SkBitmap& processed) { | |
| 79 const std::string key = GetKey(src, size); | |
| 80 cache_[key].image = processed; | |
| 81 cache_[key].used = true; | |
| 82 } | |
| 83 | |
| 84 IconCache::IconCache() { | |
| 85 } | |
| 86 | |
| 87 IconCache::~IconCache() { | |
| 88 } | |
| 89 | |
| 90 } // namespace ash | |
| OLD | NEW |