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 <map> |
8 #include <windows.h> | 8 #include <windows.h> |
9 | 9 |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 | 48 |
49 private: | 49 private: |
50 typedef std::map<std::wstring, std::vector<char> > DeviceColorProfile; | 50 typedef std::map<std::wstring, std::vector<char> > DeviceColorProfile; |
51 | 51 |
52 DeviceColorProfile cache_; | 52 DeviceColorProfile cache_; |
53 base::Lock lock_; | 53 base::Lock lock_; |
54 | 54 |
55 DISALLOW_COPY_AND_ASSIGN(ColorProfileCache); | 55 DISALLOW_COPY_AND_ASSIGN(ColorProfileCache); |
56 }; | 56 }; |
57 | 57 |
| 58 base::LazyInstance<ColorProfileCache>::Leaky g_color_profile_cache = |
| 59 LAZY_INSTANCE_INITIALIZER; |
| 60 |
| 61 inline ColorProfileCache& GetColorProfileCache() { |
| 62 return g_color_profile_cache.Get(); |
| 63 } |
| 64 |
58 bool GetDisplayColorProfile(const gfx::Rect& bounds, | 65 bool GetDisplayColorProfile(const gfx::Rect& bounds, |
59 std::vector<char>* profile) { | 66 std::vector<char>* profile) { |
60 if (bounds.IsEmpty()) | 67 DCHECK(profile->empty()); |
| 68 |
| 69 RECT rect = bounds.ToRECT(); |
| 70 HMONITOR handle = ::MonitorFromRect(&rect, MONITOR_DEFAULTTONULL); |
| 71 if (bounds.IsEmpty() || !handle) |
61 return false; | 72 return false; |
62 // TODO(noel): implement. | 73 |
63 return false; | 74 MONITORINFOEX monitor; |
| 75 monitor.cbSize = sizeof(MONITORINFOEX); |
| 76 CHECK(::GetMonitorInfo(handle, &monitor)); |
| 77 if (GetColorProfileCache().Find(monitor.szDevice, profile)) |
| 78 return true; |
| 79 |
| 80 HDC hdc = ::CreateDC(monitor.szDevice, NULL, NULL, NULL); |
| 81 DWORD path_length = MAX_PATH; |
| 82 WCHAR path[MAX_PATH + 1]; |
| 83 BOOL result = ::GetICMProfile(hdc, &path_length, path); |
| 84 ::DeleteDC(hdc); |
| 85 if (!result) |
| 86 return false; |
| 87 |
| 88 base::FilePath file_name = base::FilePath(path).BaseName(); |
| 89 if (file_name != base::FilePath(L"sRGB Color Space Profile.icm")) { |
| 90 std::string data; |
| 91 if (base::ReadFileToString(base::FilePath(path), &data)) |
| 92 profile->assign(data.data(), data.data() + data.size()); |
| 93 size_t length = profile->size(); |
| 94 if (gfx::InvalidColorProfileLength(length)) |
| 95 profile->clear(); |
| 96 } |
| 97 |
| 98 GetColorProfileCache().Insert(monitor.szDevice, *profile); |
| 99 return true; |
64 } | 100 } |
65 | 101 |
66 void ReadColorProfile(std::vector<char>* profile) { | 102 void ReadColorProfile(std::vector<char>* profile) { |
67 // TODO: support multiple monitors. | 103 // TODO: support multiple monitors. |
68 HDC screen_dc = GetDC(NULL); | 104 HDC screen_dc = GetDC(NULL); |
69 DWORD path_len = MAX_PATH; | 105 DWORD path_len = MAX_PATH; |
70 WCHAR path[MAX_PATH + 1]; | 106 WCHAR path[MAX_PATH + 1]; |
71 | 107 |
72 BOOL result = GetICMProfile(screen_dc, &path_len, path); | 108 BOOL result = GetICMProfile(screen_dc, &path_len, path); |
73 ReleaseDC(NULL, screen_dc); | 109 ReleaseDC(NULL, screen_dc); |
74 if (!result) | 110 if (!result) |
75 return; | 111 return; |
76 std::string profileData; | 112 std::string profileData; |
77 if (!base::ReadFileToString(base::FilePath(path), &profileData)) | 113 if (!base::ReadFileToString(base::FilePath(path), &profileData)) |
78 return; | 114 return; |
79 size_t length = profileData.size(); | 115 size_t length = profileData.size(); |
80 if (gfx::InvalidColorProfileLength(length)) | 116 if (gfx::InvalidColorProfileLength(length)) |
81 return; | 117 return; |
82 profile->assign(profileData.data(), profileData.data() + length); | 118 profile->assign(profileData.data(), profileData.data() + length); |
83 } | 119 } |
84 | 120 |
85 } // namespace gfx | 121 } // namespace gfx |
OLD | NEW |