Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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/gfx/icc_profile.h" | |
| 6 | |
| 7 #include <list> | |
| 8 | |
| 9 #include "base/containers/mru_cache.h" | |
| 10 #include "base/lazy_instance.h" | |
| 11 #include "base/synchronization/lock.h" | |
| 12 | |
| 13 namespace gfx { | |
| 14 | |
| 15 namespace { | |
| 16 const size_t kMinProfileLength = 128; | |
| 17 const size_t kMaxProfileLength = 4 * 1024 * 1024; | |
| 18 | |
| 19 // Allow keeping around a maximum of 8 cached ICC profiles. Beware that | |
| 20 // we will do a linear search thorugh currently-cached ICC profiles, | |
| 21 // when creating a new ICC profile. | |
| 22 const size_t kMaxCachedICCProfiles = 8; | |
| 23 | |
| 24 struct Cache { | |
| 25 Cache() : id_to_icc_profile_mru(kMaxCachedICCProfiles) {} | |
| 26 ~Cache() {} | |
| 27 | |
| 28 // Start from-ICC-data IDs at the end of the hard-coded list. | |
| 29 uint64_t next_unused_id = 5; | |
|
dcheng
2016/07/25 13:42:26
Dumb question: how do the initial hardcoded entrie
ccameron
2016/07/25 21:31:54
They're part of the TODO -- we don't compute them
| |
| 30 base::MRUCache<uint64_t, ICCProfile> id_to_icc_profile_mru; | |
| 31 base::Lock lock; | |
| 32 }; | |
| 33 static base::LazyInstance<Cache> g_cache; | |
| 34 | |
| 35 } // namespace | |
| 36 | |
| 37 ICCProfile::ICCProfile() = default; | |
| 38 ICCProfile::ICCProfile(ICCProfile&& other) = default; | |
| 39 ICCProfile::ICCProfile(const ICCProfile& other) = default; | |
| 40 ICCProfile& ICCProfile::operator=(const ICCProfile& other) = default; | |
| 41 ICCProfile::~ICCProfile() = default; | |
| 42 | |
| 43 bool ICCProfile::operator==(const ICCProfile& other) const { | |
| 44 return valid_ == other.valid_ && data_ == other.data_; | |
| 45 } | |
| 46 | |
| 47 // static | |
| 48 ICCProfile ICCProfile::FromData(const std::vector<char>& icc_profile_data) { | |
| 49 ICCProfile icc_profile; | |
| 50 if (IsValidProfileLength(icc_profile_data.size())) { | |
| 51 icc_profile.valid_ = true; | |
| 52 icc_profile.data_ = icc_profile_data; | |
| 53 } | |
| 54 if (!icc_profile.valid_) | |
| 55 return icc_profile; | |
| 56 | |
| 57 Cache& cache = g_cache.Get(); | |
| 58 base::AutoLock lock(cache.lock); | |
| 59 | |
| 60 // Linearly search the cached ICC profiles to find one with the same data. | |
| 61 // If it exists, re-use its id and touch it in the cache. | |
| 62 for (auto iter = cache.id_to_icc_profile_mru.begin(); | |
| 63 iter != cache.id_to_icc_profile_mru.end(); ++iter) { | |
| 64 if (icc_profile.data_ == iter->second.data_) { | |
| 65 icc_profile.id_ = iter->second.id_; | |
| 66 cache.id_to_icc_profile_mru.Get(icc_profile.id_); | |
| 67 return icc_profile; | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 // Create a new cached id and add it to the cache. | |
| 72 icc_profile.id_ = cache.next_unused_id++; | |
| 73 cache.id_to_icc_profile_mru.Put(icc_profile.id_, icc_profile); | |
| 74 return icc_profile; | |
| 75 } | |
| 76 | |
| 77 #if !defined(OS_WIN) && !defined(OS_MACOSX) | |
| 78 // static | |
| 79 ICCProfile ICCProfile::FromBestMonitor() { | |
| 80 return ICCProfile(); | |
| 81 } | |
| 82 #endif | |
| 83 | |
| 84 // static | |
| 85 ICCProfile ICCProfile::FromColorSpace(const gfx::ColorSpace& color_space) { | |
| 86 // Retrieve ICC profiles from the cache. | |
| 87 if (color_space.icc_profile_id_) { | |
| 88 Cache& cache = g_cache.Get(); | |
| 89 base::AutoLock lock(cache.lock); | |
| 90 | |
| 91 auto found = cache.id_to_icc_profile_mru.Get(color_space.icc_profile_id_); | |
| 92 if (found != cache.id_to_icc_profile_mru.end()) | |
| 93 return found->second; | |
| 94 } | |
| 95 // TODO(ccameron): Support constructing ICC profiles from arbitrary ColorSpace | |
| 96 // objects. | |
| 97 return ICCProfile(); | |
| 98 } | |
| 99 | |
| 100 const std::vector<char>& ICCProfile::GetData() const { | |
| 101 return data_; | |
| 102 } | |
| 103 | |
| 104 ColorSpace ICCProfile::GetColorSpace() const { | |
| 105 ColorSpace color_space; | |
| 106 color_space.valid_ = true; | |
| 107 color_space.icc_profile_id_ = id_; | |
| 108 | |
| 109 // Move this ICC profile to the most recently used end of the cache. | |
| 110 { | |
| 111 Cache& cache = g_cache.Get(); | |
| 112 base::AutoLock lock(cache.lock); | |
| 113 | |
| 114 auto found = cache.id_to_icc_profile_mru.Get(id_); | |
| 115 if (found == cache.id_to_icc_profile_mru.end()) | |
| 116 cache.id_to_icc_profile_mru.Put(id_, *this); | |
| 117 } | |
| 118 return color_space; | |
| 119 } | |
| 120 | |
| 121 // static | |
| 122 bool ICCProfile::IsValidProfileLength(size_t length) { | |
| 123 return length >= kMinProfileLength && length <= kMaxProfileLength; | |
| 124 } | |
| 125 | |
| 126 } // namespace gfx | |
| OLD | NEW |