| 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 "ui/app_list/icon_cache.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #include "base/md5.h" | |
| 11 #include "ui/gfx/size.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // Predicator for sorting ImageSkiaRep by scale factor. | |
| 16 bool ImageRepScaleFactorCompare(const gfx::ImageSkiaRep& rep1, | |
| 17 const gfx::ImageSkiaRep& rep2) { | |
| 18 return rep1.scale_factor() < rep2.scale_factor(); | |
| 19 } | |
| 20 | |
| 21 // Gets cache key based on |image| contents and desired |size|. | |
| 22 std::string GetKey(const gfx::ImageSkia& image, const gfx::Size& size) { | |
| 23 gfx::ImageSkia::ImageSkiaReps image_reps = image.image_reps(); | |
| 24 DCHECK_GT(image_reps.size(), 0u); | |
| 25 | |
| 26 std::sort(image_reps.begin(), image_reps.end(), &ImageRepScaleFactorCompare); | |
| 27 | |
| 28 base::MD5Context ctx; | |
| 29 base::MD5Init(&ctx); | |
| 30 for (gfx::ImageSkia::ImageSkiaReps::const_iterator it = image_reps.begin(); | |
| 31 it != image_reps.end(); ++it) { | |
| 32 const SkBitmap& bitmap = it->sk_bitmap(); | |
| 33 SkAutoLockPixels image_lock(bitmap); | |
| 34 | |
| 35 base::MD5Update( | |
| 36 &ctx, | |
| 37 base::StringPiece(reinterpret_cast<const char*>(bitmap.getPixels()), | |
| 38 bitmap.getSize())); | |
| 39 } | |
| 40 | |
| 41 base::MD5Digest digest; | |
| 42 base::MD5Final(&digest, &ctx); | |
| 43 | |
| 44 return MD5DigestToBase16(digest) + "." + size.ToString(); | |
| 45 } | |
| 46 | |
| 47 } // namespace | |
| 48 | |
| 49 namespace app_list { | |
| 50 | |
| 51 // static | |
| 52 IconCache* IconCache::instance_ = NULL; | |
| 53 | |
| 54 // static | |
| 55 void IconCache::CreateInstance() { | |
| 56 DCHECK(!instance_); | |
| 57 instance_ = new IconCache; | |
| 58 } | |
| 59 | |
| 60 // static | |
| 61 void IconCache::DeleteInstance() { | |
| 62 DCHECK(instance_); | |
| 63 delete instance_; | |
| 64 instance_ = NULL; | |
| 65 } | |
| 66 | |
| 67 // static | |
| 68 IconCache* IconCache::GetInstance() { | |
| 69 DCHECK(instance_); | |
| 70 return instance_; | |
| 71 } | |
| 72 | |
| 73 void IconCache::MarkAllEntryUnused() { | |
| 74 for (Cache::iterator i = cache_.begin(); i != cache_.end(); ++i) | |
| 75 i->second.used = false; | |
| 76 } | |
| 77 | |
| 78 void IconCache::PurgeAllUnused() { | |
| 79 for (Cache::iterator i = cache_.begin(); i != cache_.end();) { | |
| 80 Cache::iterator current(i); | |
| 81 ++i; | |
| 82 if (!current->second.used) | |
| 83 cache_.erase(current); | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 bool IconCache::Get(const gfx::ImageSkia& src, | |
| 88 const gfx::Size& size, | |
| 89 gfx::ImageSkia* processed) { | |
| 90 Cache::iterator it = cache_.find(GetKey(src, size)); | |
| 91 if (it == cache_.end()) | |
| 92 return false; | |
| 93 | |
| 94 it->second.used = true; | |
| 95 | |
| 96 if (processed) | |
| 97 *processed = it->second.image; | |
| 98 return true; | |
| 99 } | |
| 100 | |
| 101 void IconCache::Put(const gfx::ImageSkia& src, | |
| 102 const gfx::Size& size, | |
| 103 const gfx::ImageSkia& processed) { | |
| 104 const std::string key = GetKey(src, size); | |
| 105 cache_[key].image = processed; | |
| 106 cache_[key].used = true; | |
| 107 } | |
| 108 | |
| 109 IconCache::IconCache() { | |
| 110 } | |
| 111 | |
| 112 IconCache::~IconCache() { | |
| 113 } | |
| 114 | |
| 115 } // namespace app_list | |
| OLD | NEW |