Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(805)

Side by Side Diff: ui/gfx/color_space.cc

Issue 2150883002: Revert of Color: Don't duplicate ICC profile data (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ui/gfx/color_space.h ('k') | ui/gfx/color_space_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/lazy_instance.h"
10 #include "base/synchronization/lock.h"
11
12 namespace gfx { 8 namespace gfx {
13 9
14 namespace { 10 namespace {
15 static const size_t kMinProfileLength = 128; 11 static const size_t kMinProfileLength = 128;
16 static const size_t kMaxProfileLength = 4 * 1024 * 1024; 12 static const size_t kMaxProfileLength = 4 * 1024 * 1024;
17 } // namespace 13 }
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 static base::LazyInstance<std::map<Key, GlobalData*>> map_;
84 static base::LazyInstance<base::Lock> map_lock_;
85 };
86
87 base::LazyInstance<std::map<ColorSpace::Key, ColorSpace::GlobalData*>>
88 ColorSpace::GlobalData::map_ = LAZY_INSTANCE_INITIALIZER;
89 base::LazyInstance<base::Lock>
90 ColorSpace::GlobalData::map_lock_ = LAZY_INSTANCE_INITIALIZER;
91 14
92 ColorSpace::ColorSpace() = default; 15 ColorSpace::ColorSpace() = default;
93 ColorSpace::ColorSpace(ColorSpace&& other) = default; 16 ColorSpace::ColorSpace(ColorSpace&& other) = default;
94 ColorSpace::ColorSpace(const ColorSpace& other) = default; 17 ColorSpace::ColorSpace(const ColorSpace& other) = default;
95 ColorSpace& ColorSpace::operator=(const ColorSpace& other) = default; 18 ColorSpace& ColorSpace::operator=(const ColorSpace& other) = default;
96 ColorSpace::~ColorSpace() = default; 19 ColorSpace::~ColorSpace() = default;
97 20
98 bool ColorSpace::operator==(const ColorSpace& other) const { 21 bool ColorSpace::operator==(const ColorSpace& other) const {
99 if (type_ == Type::ICC_PROFILE && other.type_ == Type::ICC_PROFILE) 22 return icc_profile_ == other.icc_profile_;
100 return global_data_ == other.global_data_;
101 return type_ == other.type_;
102 } 23 }
103 24
104 bool ColorSpace::operator<(const ColorSpace& other) const {
105 // Note that this does a pointer-based comparision.
106 if (type_ == Type::ICC_PROFILE && other.type_ == Type::ICC_PROFILE)
107 return global_data_.get() < other.global_data_.get();
108 return type_ < other.type_;
109 }
110
111 // static
112 ColorSpace ColorSpace::FromICCProfile(const std::vector<char>& icc_profile) { 25 ColorSpace ColorSpace::FromICCProfile(const std::vector<char>& icc_profile) {
113 ColorSpace color_space; 26 ColorSpace color_space;
114 if (IsValidProfileLength(icc_profile.size())) { 27 if (IsValidProfileLength(icc_profile.size()))
115 color_space.type_ = Type::ICC_PROFILE; 28 color_space.icc_profile_ = icc_profile;
116 Key key(Type::ICC_PROFILE, icc_profile);
117 GlobalData::Get(key, &color_space.global_data_);
118 }
119 return color_space; 29 return color_space;
120 } 30 }
121 31
122 #if !defined(OS_WIN) && !defined(OS_MACOSX) && !defined(USE_X11) 32 #if !defined(OS_WIN) && !defined(OS_MACOSX) && !defined(USE_X11)
123 // static 33 // static
124 ColorSpace ColorSpace::FromBestMonitor() { 34 ColorSpace ColorSpace::FromBestMonitor() {
125 return ColorSpace(); 35 return ColorSpace();
126 } 36 }
127 #endif 37 #endif
128 38
129 const std::vector<char>& ColorSpace::GetICCProfile() const {
130 if (!global_data_) {
131 Key key(type_, std::vector<char>());
132 GlobalData::Get(key, &global_data_);
133 }
134 return global_data_->GetICCProfile();
135 }
136
137 // static 39 // static
138 bool ColorSpace::IsValidProfileLength(size_t length) { 40 bool ColorSpace::IsValidProfileLength(size_t length) {
139 return length >= kMinProfileLength && length <= kMaxProfileLength; 41 return length >= kMinProfileLength && length <= kMaxProfileLength;
140 } 42 }
141 43
142 } // namespace gfx 44 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/color_space.h ('k') | ui/gfx/color_space_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698