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/synchronization/lock.h" | |
10 | |
8 namespace gfx { | 11 namespace gfx { |
9 | 12 |
10 namespace { | 13 namespace { |
11 static const size_t kMinProfileLength = 128; | 14 static const size_t kMinProfileLength = 128; |
12 static const size_t kMaxProfileLength = 4 * 1024 * 1024; | 15 static const size_t kMaxProfileLength = 4 * 1024 * 1024; |
13 } | 16 } // namespace |
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_; | |
hubbe
2016/07/12 05:12:40
Seems like map_.erase(Key(icc_profile_)) would wor
ccameron
2016/07/12 19:11:03
True, ror now it's pretty small (we'd also need th
| |
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_; | |
Dan Beam
2016/07/13 19:15:10
i think these are adding static initializers:
http
| |
14 | 88 |
15 ColorSpace::ColorSpace() = default; | 89 ColorSpace::ColorSpace() = default; |
16 ColorSpace::ColorSpace(ColorSpace&& other) = default; | 90 ColorSpace::ColorSpace(ColorSpace&& other) = default; |
17 ColorSpace::ColorSpace(const ColorSpace& other) = default; | 91 ColorSpace::ColorSpace(const ColorSpace& other) = default; |
18 ColorSpace& ColorSpace::operator=(const ColorSpace& other) = default; | 92 ColorSpace& ColorSpace::operator=(const ColorSpace& other) = default; |
19 ColorSpace::~ColorSpace() = default; | 93 ColorSpace::~ColorSpace() = default; |
20 | 94 |
21 bool ColorSpace::operator==(const ColorSpace& other) const { | 95 bool ColorSpace::operator==(const ColorSpace& other) const { |
22 return icc_profile_ == other.icc_profile_; | 96 if (type_ == Type::ICC_PROFILE && other.type_ == Type::ICC_PROFILE) |
97 return global_data_ == other.global_data_; | |
98 return type_ == other.type_; | |
23 } | 99 } |
24 | 100 |
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 | |
25 ColorSpace ColorSpace::FromICCProfile(const std::vector<char>& icc_profile) { | 109 ColorSpace ColorSpace::FromICCProfile(const std::vector<char>& icc_profile) { |
26 ColorSpace color_space; | 110 ColorSpace color_space; |
27 if (IsValidProfileLength(icc_profile.size())) | 111 if (IsValidProfileLength(icc_profile.size())) { |
28 color_space.icc_profile_ = icc_profile; | 112 color_space.type_ = Type::ICC_PROFILE; |
113 Key key(Type::ICC_PROFILE, icc_profile); | |
114 GlobalData::Get(key, &color_space.global_data_); | |
115 } | |
29 return color_space; | 116 return color_space; |
30 } | 117 } |
31 | 118 |
32 #if !defined(OS_WIN) && !defined(OS_MACOSX) && !defined(USE_X11) | 119 #if !defined(OS_WIN) && !defined(OS_MACOSX) && !defined(USE_X11) |
33 // static | 120 // static |
34 ColorSpace ColorSpace::FromBestMonitor() { | 121 ColorSpace ColorSpace::FromBestMonitor() { |
35 return ColorSpace(); | 122 return ColorSpace(); |
36 } | 123 } |
37 #endif | 124 #endif |
38 | 125 |
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 | |
39 // static | 134 // static |
40 bool ColorSpace::IsValidProfileLength(size_t length) { | 135 bool ColorSpace::IsValidProfileLength(size_t length) { |
41 return length >= kMinProfileLength && length <= kMaxProfileLength; | 136 return length >= kMinProfileLength && length <= kMaxProfileLength; |
42 } | 137 } |
43 | 138 |
44 } // namespace gfx | 139 } // namespace gfx |
OLD | NEW |