summaryrefslogtreecommitdiffstats
path: root/ui/app_list/icon_cache.cc
blob: b0b9997e6aebc9113232521827a3a0c956fc6e43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/app_list/icon_cache.h"

#include "base/logging.h"
#include "base/md5.h"
#include "ui/gfx/size.h"

namespace {

// Gets cache key based on |image| contents and desired |size|.
std::string GetKey(const SkBitmap& image, const gfx::Size& size) {
  SkAutoLockPixels image_lock(image);
  base::MD5Digest digest;
  MD5Sum(image.getPixels(), image.getSize(), &digest);

  return MD5DigestToBase16(digest) + "." + size.ToString();
}

}  // namespace

namespace app_list {

// static
IconCache* IconCache::instance_ = NULL;

// static
void IconCache::CreateInstance() {
  DCHECK(!instance_);
  instance_ = new IconCache;
}

// static
void IconCache::DeleteInstance() {
  DCHECK(instance_);
  delete instance_;
  instance_ = NULL;
}

// static
IconCache* IconCache::GetInstance() {
  DCHECK(instance_);
  return instance_;
}

void IconCache::MarkAllEntryUnused() {
  for (Cache::iterator i = cache_.begin(); i != cache_.end(); ++i)
    i->second.used = false;
}

void IconCache::PurgeAllUnused() {
  for (Cache::iterator i = cache_.begin(); i != cache_.end();) {
    Cache::iterator current(i);
    ++i;
    if (!current->second.used)
      cache_.erase(current);
  }
}

bool IconCache::Get(const SkBitmap& src,
                    const gfx::Size& size,
                    SkBitmap* processed) {
  Cache::iterator it = cache_.find(GetKey(src, size));
  if (it == cache_.end())
    return false;

  it->second.used = true;

  if (processed)
    *processed = it->second.image;
  return true;
}

void IconCache::Put(const SkBitmap& src,
                    const gfx::Size& size,
                    const SkBitmap& processed) {
  const std::string key = GetKey(src, size);
  cache_[key].image = processed;
  cache_[key].used = true;
}

IconCache::IconCache() {
}

IconCache::~IconCache() {
}

}  // namespace app_list