OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "build/build_config.h" | |
6 #include "ui/gfx/color_space.h" | 5 #include "ui/gfx/color_space.h" |
7 | 6 |
| 7 #include <map> |
| 8 |
| 9 #include "base/lazy_instance.h" |
| 10 #include "base/synchronization/lock.h" |
| 11 |
8 namespace gfx { | 12 namespace gfx { |
9 | 13 |
10 namespace { | 14 namespace { |
11 static const size_t kMinProfileLength = 128; | 15 static const size_t kMinProfileLength = 128; |
12 static const size_t kMaxProfileLength = 4 * 1024 * 1024; | 16 static const size_t kMaxProfileLength = 4 * 1024 * 1024; |
13 } | 17 } // namespace |
| 18 |
| 19 // The structure used to look up GlobalData structures. |
| 20 struct ColorSpace::Key { |
| 21 Key(ColorSpace::Type type, const std::vector<char>& icc_profile) |
| 22 : type(type), icc_profile(icc_profile) {} |
| 23 |
| 24 bool operator<(const Key& other) const { |
| 25 if (type < other.type) |
| 26 return true; |
| 27 if (type > other.type) |
| 28 return false; |
| 29 if (type != Type::ICC_PROFILE) |
| 30 return false; |
| 31 |
| 32 if (icc_profile.size() < other.icc_profile.size()) |
| 33 return true; |
| 34 if (icc_profile.size() > other.icc_profile.size()) |
| 35 return false; |
| 36 for (size_t i = 0; i < icc_profile.size(); ++i) { |
| 37 if (icc_profile[i] < other.icc_profile[i]) |
| 38 return true; |
| 39 if (icc_profile[i] > other.icc_profile[i]) |
| 40 return false; |
| 41 } |
| 42 return false; |
| 43 } |
| 44 |
| 45 ColorSpace::Type type; |
| 46 const std::vector<char> icc_profile; |
| 47 }; |
| 48 |
| 49 // Because this structure is shared across gfx::ColorSpace objects on |
| 50 // different threads, it needs to be thread-safe. |
| 51 class ColorSpace::GlobalData |
| 52 : public base::RefCountedThreadSafe<ColorSpace::GlobalData> { |
| 53 public: |
| 54 static void Get(const Key& key, scoped_refptr<GlobalData>* value) { |
| 55 base::AutoLock lock(map_lock_.Get()); |
| 56 auto insert_result = map_.Get().insert(std::make_pair(key, nullptr)); |
| 57 if (insert_result.second) |
| 58 insert_result.first->second = new GlobalData(key, insert_result.first); |
| 59 *value = make_scoped_refptr(insert_result.first->second); |
| 60 } |
| 61 |
| 62 const std::vector<char>& GetICCProfile() const { return icc_profile_; } |
| 63 |
| 64 private: |
| 65 friend class base::RefCountedThreadSafe<GlobalData>; |
| 66 |
| 67 GlobalData(const Key& key, std::map<Key, GlobalData*>::iterator iterator) |
| 68 : iterator_(iterator) { |
| 69 // TODO: Compute the ICC profile for named color spaces. |
| 70 if (key.type == Type::ICC_PROFILE) |
| 71 icc_profile_ = key.icc_profile; |
| 72 } |
| 73 ~GlobalData() { |
| 74 base::AutoLock lock(map_lock_.Get()); |
| 75 map_.Get().erase(iterator_); |
| 76 } |
| 77 |
| 78 std::vector<char> icc_profile_; |
| 79 |
| 80 // In order to remove |this| from |map_| when its last reference goes away, |
| 81 // keep in |iterator_| the corresponding iterator in |map_|. |
| 82 std::map<Key, GlobalData*>::iterator iterator_; |
| 83 // The |map_| tracks the existing GlobalData instances, which are owned by |
| 84 // ColorSpace instances. Note that |map_| must be leaky because GlobalData |
| 85 // instances will reach back into it at unpredictable times during tear-down. |
| 86 static base::LazyInstance<std::map<Key, GlobalData*>>::Leaky map_; |
| 87 static base::LazyInstance<base::Lock>::Leaky map_lock_; |
| 88 }; |
| 89 |
| 90 base::LazyInstance<std::map<ColorSpace::Key, ColorSpace::GlobalData*>>::Leaky |
| 91 ColorSpace::GlobalData::map_ = LAZY_INSTANCE_INITIALIZER; |
| 92 base::LazyInstance<base::Lock>::Leaky |
| 93 ColorSpace::GlobalData::map_lock_ = LAZY_INSTANCE_INITIALIZER; |
14 | 94 |
15 ColorSpace::ColorSpace() = default; | 95 ColorSpace::ColorSpace() = default; |
16 ColorSpace::ColorSpace(ColorSpace&& other) = default; | 96 ColorSpace::ColorSpace(ColorSpace&& other) = default; |
17 ColorSpace::ColorSpace(const ColorSpace& other) = default; | 97 ColorSpace::ColorSpace(const ColorSpace& other) = default; |
18 ColorSpace& ColorSpace::operator=(const ColorSpace& other) = default; | 98 ColorSpace& ColorSpace::operator=(const ColorSpace& other) = default; |
19 ColorSpace::~ColorSpace() = default; | 99 ColorSpace::~ColorSpace() = default; |
20 | 100 |
21 bool ColorSpace::operator==(const ColorSpace& other) const { | 101 bool ColorSpace::operator==(const ColorSpace& other) const { |
22 return icc_profile_ == other.icc_profile_; | 102 if (type_ == Type::ICC_PROFILE && other.type_ == Type::ICC_PROFILE) |
| 103 return global_data_ == other.global_data_; |
| 104 return type_ == other.type_; |
23 } | 105 } |
24 | 106 |
| 107 bool ColorSpace::operator<(const ColorSpace& other) const { |
| 108 // Note that this does a pointer-based comparision. |
| 109 if (type_ == Type::ICC_PROFILE && other.type_ == Type::ICC_PROFILE) |
| 110 return global_data_.get() < other.global_data_.get(); |
| 111 return type_ < other.type_; |
| 112 } |
| 113 |
| 114 // static |
25 ColorSpace ColorSpace::FromICCProfile(const std::vector<char>& icc_profile) { | 115 ColorSpace ColorSpace::FromICCProfile(const std::vector<char>& icc_profile) { |
26 ColorSpace color_space; | 116 ColorSpace color_space; |
27 if (IsValidProfileLength(icc_profile.size())) | 117 if (IsValidProfileLength(icc_profile.size())) { |
28 color_space.icc_profile_ = icc_profile; | 118 color_space.type_ = Type::ICC_PROFILE; |
| 119 Key key(Type::ICC_PROFILE, icc_profile); |
| 120 GlobalData::Get(key, &color_space.global_data_); |
| 121 } |
29 return color_space; | 122 return color_space; |
30 } | 123 } |
31 | 124 |
32 #if !defined(OS_WIN) && !defined(OS_MACOSX) && !defined(USE_X11) | 125 #if !defined(OS_WIN) && !defined(OS_MACOSX) && !defined(USE_X11) |
33 // static | 126 // static |
34 ColorSpace ColorSpace::FromBestMonitor() { | 127 ColorSpace ColorSpace::FromBestMonitor() { |
35 return ColorSpace(); | 128 return ColorSpace(); |
36 } | 129 } |
37 #endif | 130 #endif |
38 | 131 |
| 132 const std::vector<char>& ColorSpace::GetICCProfile() const { |
| 133 if (!global_data_) { |
| 134 Key key(type_, std::vector<char>()); |
| 135 GlobalData::Get(key, &global_data_); |
| 136 } |
| 137 return global_data_->GetICCProfile(); |
| 138 } |
| 139 |
39 // static | 140 // static |
40 bool ColorSpace::IsValidProfileLength(size_t length) { | 141 bool ColorSpace::IsValidProfileLength(size_t length) { |
41 return length >= kMinProfileLength && length <= kMaxProfileLength; | 142 return length >= kMinProfileLength && length <= kMaxProfileLength; |
42 } | 143 } |
43 | 144 |
44 } // namespace gfx | 145 } // namespace gfx |
OLD | NEW |