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