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 // Note that |map_| must be leaky, because GlobalData instances will reach | |
84 // back into it at unpredictable times during tear-down. | |
ccameron
2016/07/14 18:42:10
The previous patch had 2 problems, one of which th
hubbe
2016/07/14 18:54:07
Might also want to explain that since the map does
| |
85 static base::LazyInstance<std::map<Key, GlobalData*>>::Leaky map_; | |
86 static base::LazyInstance<base::Lock>::Leaky map_lock_; | |
87 }; | |
88 | |
89 base::LazyInstance<std::map<ColorSpace::Key, ColorSpace::GlobalData*>>::Leaky | |
90 ColorSpace::GlobalData::map_ = LAZY_INSTANCE_INITIALIZER; | |
91 base::LazyInstance<base::Lock>::Leaky | |
92 ColorSpace::GlobalData::map_lock_ = LAZY_INSTANCE_INITIALIZER; | |
14 | 93 |
15 ColorSpace::ColorSpace() = default; | 94 ColorSpace::ColorSpace() = default; |
16 ColorSpace::ColorSpace(ColorSpace&& other) = default; | 95 ColorSpace::ColorSpace(ColorSpace&& other) = default; |
17 ColorSpace::ColorSpace(const ColorSpace& other) = default; | 96 ColorSpace::ColorSpace(const ColorSpace& other) = default; |
18 ColorSpace& ColorSpace::operator=(const ColorSpace& other) = default; | 97 ColorSpace& ColorSpace::operator=(const ColorSpace& other) = default; |
19 ColorSpace::~ColorSpace() = default; | 98 ColorSpace::~ColorSpace() = default; |
20 | 99 |
21 bool ColorSpace::operator==(const ColorSpace& other) const { | 100 bool ColorSpace::operator==(const ColorSpace& other) const { |
22 return icc_profile_ == other.icc_profile_; | 101 if (type_ == Type::ICC_PROFILE && other.type_ == Type::ICC_PROFILE) |
102 return global_data_ == other.global_data_; | |
103 return type_ == other.type_; | |
23 } | 104 } |
24 | 105 |
106 bool ColorSpace::operator<(const ColorSpace& other) const { | |
107 // Note that this does a pointer-based comparision. | |
108 if (type_ == Type::ICC_PROFILE && other.type_ == Type::ICC_PROFILE) | |
109 return global_data_.get() < other.global_data_.get(); | |
110 return type_ < other.type_; | |
111 } | |
112 | |
113 // static | |
25 ColorSpace ColorSpace::FromICCProfile(const std::vector<char>& icc_profile) { | 114 ColorSpace ColorSpace::FromICCProfile(const std::vector<char>& icc_profile) { |
26 ColorSpace color_space; | 115 ColorSpace color_space; |
27 if (IsValidProfileLength(icc_profile.size())) | 116 if (IsValidProfileLength(icc_profile.size())) { |
28 color_space.icc_profile_ = icc_profile; | 117 color_space.type_ = Type::ICC_PROFILE; |
118 Key key(Type::ICC_PROFILE, icc_profile); | |
119 GlobalData::Get(key, &color_space.global_data_); | |
120 } | |
29 return color_space; | 121 return color_space; |
30 } | 122 } |
31 | 123 |
32 #if !defined(OS_WIN) && !defined(OS_MACOSX) && !defined(USE_X11) | 124 #if !defined(OS_WIN) && !defined(OS_MACOSX) && !defined(USE_X11) |
33 // static | 125 // static |
34 ColorSpace ColorSpace::FromBestMonitor() { | 126 ColorSpace ColorSpace::FromBestMonitor() { |
35 return ColorSpace(); | 127 return ColorSpace(); |
36 } | 128 } |
37 #endif | 129 #endif |
38 | 130 |
131 const std::vector<char>& ColorSpace::GetICCProfile() const { | |
132 if (!global_data_) { | |
133 Key key(type_, std::vector<char>()); | |
134 GlobalData::Get(key, &global_data_); | |
135 } | |
136 return global_data_->GetICCProfile(); | |
137 } | |
138 | |
39 // static | 139 // static |
40 bool ColorSpace::IsValidProfileLength(size_t length) { | 140 bool ColorSpace::IsValidProfileLength(size_t length) { |
41 return length >= kMinProfileLength && length <= kMaxProfileLength; | 141 return length >= kMinProfileLength && length <= kMaxProfileLength; |
42 } | 142 } |
43 | 143 |
44 } // namespace gfx | 144 } // namespace gfx |
OLD | NEW |