Chromium Code Reviews| 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 found = map_.find(key); | |
|
hubbe
2016/07/11 22:51:59
use insert here
ccameron
2016/07/12 02:49:01
Done.
| |
| 56 if (found != map_.end()) { | |
| 57 *value = make_scoped_refptr(found->second); | |
| 58 return; | |
| 59 } | |
| 60 GlobalData* global_data = new GlobalData(key); | |
| 61 map_.insert(std::make_pair(key, global_data)); | |
| 62 *value = make_scoped_refptr(global_data); | |
| 63 } | |
| 64 | |
| 65 const std::vector<char>& GetICCProfile() const { return key_.icc_profile; } | |
| 66 | |
| 67 private: | |
| 68 friend class base::RefCountedThreadSafe<GlobalData>; | |
| 69 | |
| 70 GlobalData(const Key& key) : key_(key) { | |
| 71 // TODO: Compute the ICC profile for typed color spaces here. | |
| 72 } | |
| 73 ~GlobalData() { | |
| 74 base::AutoLock lock(map_lock_); | |
| 75 auto found = map_.find(key_); | |
| 76 DCHECK(found != map_.end()); | |
| 77 DCHECK(found->second == this); | |
| 78 map_.erase(found); | |
| 79 } | |
| 80 | |
| 81 Key key_; | |
|
hubbe
2016/07/11 22:51:59
I think this is really confusing, because the key
ccameron
2016/07/12 02:49:00
Good point. We need to be able to erase |this| fro
| |
| 82 | |
| 83 static std::map<Key, GlobalData*> map_; | |
|
hubbe
2016/07/11 22:51:59
I think this should be hash_set<scoped_refptr<Glob
ccameron
2016/07/12 02:49:00
The hash_set would then keep a reference to the Gl
| |
| 84 static base::Lock map_lock_; | |
| 85 }; | |
| 86 | |
| 87 std::map<ColorSpace::Key, ColorSpace::GlobalData*> ColorSpace::GlobalData::map_; | |
| 88 base::Lock ColorSpace::GlobalData::map_lock_; | |
| 14 | 89 |
| 15 ColorSpace::ColorSpace() = default; | 90 ColorSpace::ColorSpace() = default; |
| 16 ColorSpace::ColorSpace(ColorSpace&& other) = default; | 91 ColorSpace::ColorSpace(ColorSpace&& other) = default; |
| 17 ColorSpace::ColorSpace(const ColorSpace& other) = default; | 92 ColorSpace::ColorSpace(const ColorSpace& other) = default; |
| 18 ColorSpace& ColorSpace::operator=(const ColorSpace& other) = default; | 93 ColorSpace& ColorSpace::operator=(const ColorSpace& other) = default; |
| 19 ColorSpace::~ColorSpace() = default; | 94 ColorSpace::~ColorSpace() = default; |
| 20 | 95 |
| 21 bool ColorSpace::operator==(const ColorSpace& other) const { | 96 bool ColorSpace::operator==(const ColorSpace& other) const { |
| 22 return icc_profile_ == other.icc_profile_; | 97 if (type_ == Type::ICC_PROFILE && other.type_ == Type::ICC_PROFILE) |
| 98 return global_data_ == other.global_data_; | |
| 99 return type_ == other.type_; | |
| 23 } | 100 } |
| 24 | 101 |
| 102 bool ColorSpace::operator<(const ColorSpace& other) const { | |
| 103 // Note that this does a pointer-based comparision. | |
| 104 if (type_ == Type::ICC_PROFILE && other.type_ == Type::ICC_PROFILE) | |
| 105 return global_data_.get() < other.global_data_.get(); | |
| 106 return type_ < other.type_; | |
| 107 } | |
| 108 | |
| 109 // static | |
| 25 ColorSpace ColorSpace::FromICCProfile(const std::vector<char>& icc_profile) { | 110 ColorSpace ColorSpace::FromICCProfile(const std::vector<char>& icc_profile) { |
| 26 ColorSpace color_space; | 111 ColorSpace color_space; |
| 27 if (IsValidProfileLength(icc_profile.size())) | 112 if (IsValidProfileLength(icc_profile.size())) { |
| 28 color_space.icc_profile_ = icc_profile; | 113 color_space.type_ = Type::ICC_PROFILE; |
| 114 Key key(Type::ICC_PROFILE, icc_profile); | |
| 115 GlobalData::Get(key, &color_space.global_data_); | |
| 116 } | |
| 29 return color_space; | 117 return color_space; |
| 30 } | 118 } |
| 31 | 119 |
| 32 #if !defined(OS_WIN) && !defined(OS_MACOSX) && !defined(USE_X11) | 120 #if !defined(OS_WIN) && !defined(OS_MACOSX) && !defined(USE_X11) |
| 33 // static | 121 // static |
| 34 ColorSpace ColorSpace::FromBestMonitor() { | 122 ColorSpace ColorSpace::FromBestMonitor() { |
| 35 return ColorSpace(); | 123 return ColorSpace(); |
| 36 } | 124 } |
| 37 #endif | 125 #endif |
| 38 | 126 |
| 127 const std::vector<char>& ColorSpace::GetICCProfile() const { | |
| 128 if (!global_data_) { | |
| 129 Key key(type_, std::vector<char>()); | |
| 130 GlobalData::Get(key, &global_data_); | |
| 131 } | |
| 132 return global_data_->GetICCProfile(); | |
| 133 } | |
| 134 | |
| 39 // static | 135 // static |
| 40 bool ColorSpace::IsValidProfileLength(size_t length) { | 136 bool ColorSpace::IsValidProfileLength(size_t length) { |
| 41 return length >= kMinProfileLength && length <= kMaxProfileLength; | 137 return length >= kMinProfileLength && length <= kMaxProfileLength; |
| 42 } | 138 } |
| 43 | 139 |
| 44 } // namespace gfx | 140 } // namespace gfx |
| OLD | NEW |