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_profile.h" | 5 #include "ui/gfx/color_profile.h" |
6 | 6 |
| 7 #include <map> |
7 #include <windows.h> | 8 #include <windows.h> |
8 | 9 |
9 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| 11 #include "base/lazy_instance.h" |
| 12 #include "base/synchronization/lock.h" |
10 | 13 |
11 namespace gfx { | 14 namespace gfx { |
12 | 15 |
| 16 class ColorProfileCache { |
| 17 public: |
| 18 // A thread-safe cache of color profiles keyed by windows device name. |
| 19 ColorProfileCache() {} |
| 20 |
| 21 bool Find(const std::wstring& device, std::vector<char>* profile) { |
| 22 base::AutoLock lock(lock_); |
| 23 DeviceColorProfile::const_iterator it = cache_.find(device); |
| 24 if (it == cache_.end()) |
| 25 return false; |
| 26 *profile = it->second; |
| 27 return true; |
| 28 } |
| 29 |
| 30 void Insert(const std::wstring& device, const std::vector<char>& profile) { |
| 31 base::AutoLock lock(lock_); |
| 32 cache_[device] = profile; |
| 33 } |
| 34 |
| 35 bool Erase(const std::wstring& device) { |
| 36 base::AutoLock lock(lock_); |
| 37 DeviceColorProfile::iterator it = cache_.find(device); |
| 38 if (it == cache_.end()) |
| 39 return false; |
| 40 cache_.erase(device); |
| 41 return true; |
| 42 } |
| 43 |
| 44 void Clear() { |
| 45 base::AutoLock lock(lock_); |
| 46 cache_.clear(); |
| 47 } |
| 48 |
| 49 private: |
| 50 typedef std::map<std::wstring, std::vector<char> > DeviceColorProfile; |
| 51 |
| 52 DeviceColorProfile cache_; |
| 53 base::Lock lock_; |
| 54 |
| 55 DISALLOW_COPY_AND_ASSIGN(ColorProfileCache); |
| 56 }; |
| 57 |
13 bool GetDisplayColorProfile(const gfx::Rect& bounds, | 58 bool GetDisplayColorProfile(const gfx::Rect& bounds, |
14 std::vector<char>* profile) { | 59 std::vector<char>* profile) { |
15 if (bounds.IsEmpty()) | 60 if (bounds.IsEmpty()) |
16 return false; | 61 return false; |
17 // TODO(noel): implement. | 62 // TODO(noel): implement. |
18 return false; | 63 return false; |
19 } | 64 } |
20 | 65 |
21 void ReadColorProfile(std::vector<char>* profile) { | 66 void ReadColorProfile(std::vector<char>* profile) { |
22 // TODO: support multiple monitors. | 67 // TODO: support multiple monitors. |
23 HDC screen_dc = GetDC(NULL); | 68 HDC screen_dc = GetDC(NULL); |
24 DWORD path_len = MAX_PATH; | 69 DWORD path_len = MAX_PATH; |
25 WCHAR path[MAX_PATH + 1]; | 70 WCHAR path[MAX_PATH + 1]; |
26 | 71 |
27 BOOL result = GetICMProfile(screen_dc, &path_len, path); | 72 BOOL result = GetICMProfile(screen_dc, &path_len, path); |
28 ReleaseDC(NULL, screen_dc); | 73 ReleaseDC(NULL, screen_dc); |
29 if (!result) | 74 if (!result) |
30 return; | 75 return; |
31 std::string profileData; | 76 std::string profileData; |
32 if (!base::ReadFileToString(base::FilePath(path), &profileData)) | 77 if (!base::ReadFileToString(base::FilePath(path), &profileData)) |
33 return; | 78 return; |
34 size_t length = profileData.size(); | 79 size_t length = profileData.size(); |
35 if (gfx::InvalidColorProfileLength(length)) | 80 if (gfx::InvalidColorProfileLength(length)) |
36 return; | 81 return; |
37 profile->assign(profileData.data(), profileData.data() + length); | 82 profile->assign(profileData.data(), profileData.data() + length); |
38 } | 83 } |
39 | 84 |
40 } // namespace gfx | 85 } // namespace gfx |
OLD | NEW |