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/lazy_instance.h" | |
| 10 #include "base/synchronization/lock.h" | |
| 11 | |
| 12 namespace gfx { | |
| 13 | |
| 14 namespace { | |
| 15 const size_t kMinProfileLength = 128; | |
| 16 const size_t kMaxProfileLength = 4 * 1024 * 1024; | |
| 17 | |
| 18 // Allow keeping around a maximum of 8 cached ICC profiles. Beware that | |
| 19 // we do lots of linear-time operations here, so do not increase this | |
| 20 // without optimizing it. | |
| 21 const size_t kMaxCachedICCProfiles = 8; | |
| 22 static base::LazyInstance<std::list<ICCProfile>> g_mru_cache; | |
|
dcheng
2016/07/22 05:00:27
FWIW, we do have some MRU cache abstractions in //
ccameron
2016/07/22 21:02:41
Oh, that's nice! Switched to using them.
| |
| 23 static base::LazyInstance<base::Lock> g_mru_cache_lock; | |
| 24 | |
| 25 uint64_t g_next_id = 1; | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 ICCProfile::ICCProfile() = default; | |
| 30 ICCProfile::ICCProfile(ICCProfile&& other) = default; | |
| 31 ICCProfile::ICCProfile(const ICCProfile& other) = default; | |
| 32 ICCProfile& ICCProfile::operator=(const ICCProfile& other) = default; | |
| 33 ICCProfile::~ICCProfile() = default; | |
| 34 | |
| 35 bool ICCProfile::operator==(const ICCProfile& other) const { | |
| 36 return valid_ == other.valid_ && data_ == other.data_; | |
| 37 } | |
| 38 | |
| 39 // static | |
| 40 ICCProfile ICCProfile::FromData(const std::vector<char>& icc_profile_data) { | |
| 41 ICCProfile icc_profile; | |
| 42 if (IsValidProfileLength(icc_profile_data.size())) { | |
| 43 icc_profile.valid_ = true; | |
| 44 icc_profile.data_ = icc_profile_data; | |
| 45 } | |
| 46 // Move this ICC profile to the front of the cache, and use its cached ID if | |
| 47 // it's already in the cache. | |
| 48 if (icc_profile.valid_) { | |
| 49 base::AutoLock lock(g_mru_cache_lock.Get()); | |
| 50 for (auto iter = g_mru_cache.Get().begin(); iter != g_mru_cache.Get().end(); | |
| 51 ++iter) { | |
| 52 if (*iter == icc_profile) { | |
| 53 icc_profile.id_ = iter->id_; | |
| 54 g_mru_cache.Get().splice(g_mru_cache.Get().end(), g_mru_cache.Get(), | |
| 55 iter); | |
| 56 return icc_profile; | |
| 57 } | |
| 58 } | |
| 59 icc_profile.id_ = g_next_id++; | |
| 60 g_mru_cache.Get().push_back(icc_profile); | |
| 61 while (g_mru_cache.Get().size() > kMaxCachedICCProfiles) | |
| 62 g_mru_cache.Get().pop_front(); | |
| 63 } | |
| 64 return icc_profile; | |
| 65 } | |
| 66 | |
| 67 #if !defined(OS_WIN) && !defined(OS_MACOSX) | |
| 68 // static | |
| 69 ICCProfile ICCProfile::FromBestMonitor() { | |
| 70 return ICCProfile(); | |
| 71 } | |
| 72 #endif | |
| 73 | |
| 74 // static | |
| 75 ICCProfile ICCProfile::FromColorSpace(const gfx::ColorSpace& color_space) { | |
| 76 // Retrieve ICC profiles from the cache. | |
| 77 if (color_space.icc_profile_id_) { | |
| 78 base::AutoLock lock(g_mru_cache_lock.Get()); | |
| 79 for (auto iter = g_mru_cache.Get().begin(); iter != g_mru_cache.Get().end(); | |
| 80 ++iter) { | |
| 81 if (iter->id_ == color_space.icc_profile_id_) { | |
| 82 g_mru_cache.Get().splice(g_mru_cache.Get().end(), g_mru_cache.Get(), | |
| 83 iter); | |
| 84 return *iter; | |
| 85 } | |
| 86 } | |
| 87 } | |
| 88 // TODO(ccameron): Support constructing ICC profiles from arbitrary ColorSpace | |
| 89 // objects. | |
|
dcheng
2016/07/22 05:00:27
There are plans to start sending this from less-pr
ccameron
2016/07/22 21:02:41
Full plan:
We'll only be sending gfx::ICCProfile
| |
| 90 return ICCProfile(); | |
| 91 } | |
| 92 | |
| 93 const std::vector<char>& ICCProfile::GetData() const { | |
| 94 return data_; | |
| 95 } | |
| 96 | |
| 97 ColorSpace ICCProfile::GetColorSpace() const { | |
| 98 ColorSpace color_space; | |
| 99 color_space.valid_ = true; | |
| 100 color_space.icc_profile_id_ = id_; | |
| 101 | |
| 102 // Move this ICC profile to the most recently used end of the cache. | |
| 103 base::AutoLock lock(g_mru_cache_lock.Get()); | |
| 104 for (auto iter = g_mru_cache.Get().begin(); iter != g_mru_cache.Get().end(); | |
| 105 ++iter) { | |
| 106 if (iter->id_ == id_) { | |
| 107 g_mru_cache.Get().splice(g_mru_cache.Get().end(), g_mru_cache.Get(), | |
| 108 iter); | |
| 109 return color_space; | |
| 110 } | |
| 111 } | |
| 112 g_mru_cache.Get().push_back(*this); | |
| 113 while (g_mru_cache.Get().size() > kMaxCachedICCProfiles) | |
| 114 g_mru_cache.Get().pop_front(); | |
| 115 return color_space; | |
| 116 } | |
| 117 | |
| 118 // static | |
| 119 bool ICCProfile::IsValidProfileLength(size_t length) { | |
| 120 return length >= kMinProfileLength && length <= kMaxProfileLength; | |
| 121 } | |
| 122 | |
| 123 } // namespace gfx | |
| OLD | NEW |