| 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 "ui/gfx/color_space.h" | 5 #include "ui/gfx/color_space.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
| 11 #include "ui/gfx/icc_profile.h" |
| 11 | 12 |
| 12 namespace gfx { | 13 namespace gfx { |
| 13 | 14 |
| 14 namespace { | 15 ColorSpace::ColorSpace() = default; |
| 15 static const size_t kMinProfileLength = 128; | |
| 16 static const size_t kMaxProfileLength = 4 * 1024 * 1024; | |
| 17 } // namespace | |
| 18 | 16 |
| 19 // The structure used to look up GlobalData structures. | 17 // static |
| 20 struct ColorSpace::Key { | 18 ColorSpace ColorSpace::CreateSRGB() { |
| 21 Key(ColorSpace::Type type, const std::vector<char>& icc_profile) | 19 ColorSpace color_space; |
| 22 : type(type), icc_profile(icc_profile) {} | 20 color_space.valid_ = true; |
| 23 | 21 color_space.icc_profile_id_ = ICCProfile::kSRGBId; |
| 24 bool operator<(const Key& other) const { | 22 return color_space; |
| 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 // The |map_| tracks the existing GlobalData instances, which are owned by | |
| 84 // ColorSpace instances. Note that |map_| must be leaky because GlobalData | |
| 85 // instances will reach back into it at unpredictable times during tear-down. | |
| 86 static base::LazyInstance<std::map<Key, GlobalData*>>::Leaky map_; | |
| 87 static base::LazyInstance<base::Lock>::Leaky map_lock_; | |
| 88 }; | |
| 89 | |
| 90 base::LazyInstance<std::map<ColorSpace::Key, ColorSpace::GlobalData*>>::Leaky | |
| 91 ColorSpace::GlobalData::map_ = LAZY_INSTANCE_INITIALIZER; | |
| 92 base::LazyInstance<base::Lock>::Leaky | |
| 93 ColorSpace::GlobalData::map_lock_ = LAZY_INSTANCE_INITIALIZER; | |
| 94 | |
| 95 ColorSpace::ColorSpace() = default; | |
| 96 ColorSpace::ColorSpace(ColorSpace&& other) = default; | |
| 97 ColorSpace::ColorSpace(const ColorSpace& other) = default; | |
| 98 ColorSpace& ColorSpace::operator=(const ColorSpace& other) = default; | |
| 99 ColorSpace::~ColorSpace() = default; | |
| 100 | |
| 101 bool ColorSpace::operator==(const ColorSpace& other) const { | |
| 102 if (type_ == Type::ICC_PROFILE && other.type_ == Type::ICC_PROFILE) | |
| 103 return global_data_ == other.global_data_; | |
| 104 return type_ == other.type_; | |
| 105 } | |
| 106 | |
| 107 bool ColorSpace::operator<(const ColorSpace& other) const { | |
| 108 // Note that this does a pointer-based comparision. | |
| 109 if (type_ == Type::ICC_PROFILE && other.type_ == Type::ICC_PROFILE) | |
| 110 return global_data_.get() < other.global_data_.get(); | |
| 111 return type_ < other.type_; | |
| 112 } | 23 } |
| 113 | 24 |
| 114 // static | 25 // static |
| 115 ColorSpace ColorSpace::FromICCProfile(const std::vector<char>& icc_profile) { | 26 ColorSpace ColorSpace::CreateJpeg() { |
| 116 ColorSpace color_space; | 27 ColorSpace color_space; |
| 117 if (IsValidProfileLength(icc_profile.size())) { | 28 color_space.valid_ = true; |
| 118 color_space.type_ = Type::ICC_PROFILE; | 29 color_space.icc_profile_id_ = ICCProfile::kJpegId; |
| 119 Key key(Type::ICC_PROFILE, icc_profile); | |
| 120 GlobalData::Get(key, &color_space.global_data_); | |
| 121 } | |
| 122 return color_space; | 30 return color_space; |
| 123 } | 31 } |
| 124 | 32 |
| 125 #if !defined(OS_WIN) && !defined(OS_MACOSX) | |
| 126 // static | 33 // static |
| 127 ColorSpace ColorSpace::FromBestMonitor() { | 34 ColorSpace ColorSpace::CreateREC601() { |
| 128 return ColorSpace(); | 35 ColorSpace color_space; |
| 129 } | 36 color_space.valid_ = true; |
| 130 #endif | 37 color_space.icc_profile_id_ = ICCProfile::kRec601Id; |
| 131 | 38 return color_space; |
| 132 const std::vector<char>& ColorSpace::GetICCProfile() const { | |
| 133 if (!global_data_) { | |
| 134 Key key(type_, std::vector<char>()); | |
| 135 GlobalData::Get(key, &global_data_); | |
| 136 } | |
| 137 return global_data_->GetICCProfile(); | |
| 138 } | 39 } |
| 139 | 40 |
| 140 // static | 41 // static |
| 141 bool ColorSpace::IsValidProfileLength(size_t length) { | 42 ColorSpace ColorSpace::CreateREC709() { |
| 142 return length >= kMinProfileLength && length <= kMaxProfileLength; | 43 ColorSpace color_space; |
| 44 color_space.valid_ = true; |
| 45 color_space.icc_profile_id_ = ICCProfile::kRec709Id; |
| 46 return color_space; |
| 47 } |
| 48 |
| 49 bool ColorSpace::operator==(const ColorSpace& other) const { |
| 50 // TODO(ccameron): The |icc_profile_id_| should eventually not factor in |
| 51 // to equality comparison at all, because it's just an optimization, but |
| 52 // for now there is no other data in this structure. |
| 53 return valid_ == other.valid_ && icc_profile_id_ == other.icc_profile_id_; |
| 143 } | 54 } |
| 144 | 55 |
| 145 } // namespace gfx | 56 } // namespace gfx |
| OLD | NEW |