| 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/icc_profile.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <map> | 9 #include <map> |
| 10 | 10 |
| 11 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 | 15 |
| 16 namespace gfx { | 16 namespace gfx { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 void ReadBestMonitorICCProfile(std::vector<char>* profile) { | 20 void ReadBestMonitorICCProfile(std::vector<char>* profile) { |
| 21 HDC screen_dc = GetDC(NULL); | 21 HDC screen_dc = GetDC(NULL); |
| 22 DWORD path_len = MAX_PATH; | 22 DWORD path_len = MAX_PATH; |
| 23 WCHAR path[MAX_PATH + 1]; | 23 WCHAR path[MAX_PATH + 1]; |
| 24 | 24 |
| 25 BOOL result = GetICMProfile(screen_dc, &path_len, path); | 25 BOOL result = GetICMProfile(screen_dc, &path_len, path); |
| 26 ReleaseDC(NULL, screen_dc); | 26 ReleaseDC(NULL, screen_dc); |
| 27 if (!result) | 27 if (!result) |
| 28 return; | 28 return; |
| 29 std::string profile_data; | 29 std::string profile_data; |
| 30 if (!base::ReadFileToString(base::FilePath(path), &profile_data)) | 30 if (!base::ReadFileToString(base::FilePath(path), &profile_data)) |
| 31 return; | 31 return; |
| 32 size_t length = profile_data.size(); | 32 size_t length = profile_data.size(); |
| 33 if (!ColorSpace::IsValidProfileLength(length)) | |
| 34 return; | |
| 35 profile->assign(profile_data.data(), profile_data.data() + length); | 33 profile->assign(profile_data.data(), profile_data.data() + length); |
| 36 } | 34 } |
| 37 | 35 |
| 38 base::LazyInstance<base::Lock> g_best_monitor_color_space_lock = | 36 base::LazyInstance<base::Lock> g_best_monitor_color_space_lock = |
| 39 LAZY_INSTANCE_INITIALIZER; | 37 LAZY_INSTANCE_INITIALIZER; |
| 40 base::LazyInstance<gfx::ColorSpace> g_best_monitor_color_space = | 38 base::LazyInstance<gfx::ICCProfile> g_best_monitor_color_space = |
| 41 LAZY_INSTANCE_INITIALIZER; | 39 LAZY_INSTANCE_INITIALIZER; |
| 42 bool g_has_initialized_best_monitor_color_space = false; | 40 bool g_has_initialized_best_monitor_color_space = false; |
| 43 | 41 |
| 44 } // namespace | 42 } // namespace |
| 45 | 43 |
| 46 // static | 44 // static |
| 47 ColorSpace ColorSpace::FromBestMonitor() { | 45 ICCProfile ICCProfile::FromBestMonitor() { |
| 48 base::AutoLock lock(g_best_monitor_color_space_lock.Get()); | 46 base::AutoLock lock(g_best_monitor_color_space_lock.Get()); |
| 49 return g_best_monitor_color_space.Get(); | 47 return g_best_monitor_color_space.Get(); |
| 50 } | 48 } |
| 51 | 49 |
| 52 // static | 50 // static |
| 53 bool ColorSpace::CachedProfilesNeedUpdate() { | 51 bool ICCProfile::CachedProfilesNeedUpdate() { |
| 54 base::AutoLock lock(g_best_monitor_color_space_lock.Get()); | 52 base::AutoLock lock(g_best_monitor_color_space_lock.Get()); |
| 55 return !g_has_initialized_best_monitor_color_space; | 53 return !g_has_initialized_best_monitor_color_space; |
| 56 } | 54 } |
| 57 | 55 |
| 58 // static | 56 // static |
| 59 void ColorSpace::UpdateCachedProfilesOnBackgroundThread() { | 57 void ICCProfile::UpdateCachedProfilesOnBackgroundThread() { |
| 60 std::vector<char> icc_profile; | 58 std::vector<char> icc_profile; |
| 61 ReadBestMonitorICCProfile(&icc_profile); | 59 ReadBestMonitorICCProfile(&icc_profile); |
| 62 gfx::ColorSpace color_space = FromICCProfile(icc_profile); | 60 gfx::ICCProfile color_space = FromData(icc_profile); |
| 63 | 61 |
| 64 base::AutoLock lock(g_best_monitor_color_space_lock.Get()); | 62 base::AutoLock lock(g_best_monitor_color_space_lock.Get()); |
| 65 g_best_monitor_color_space.Get() = color_space; | 63 g_best_monitor_color_space.Get() = color_space; |
| 66 g_has_initialized_best_monitor_color_space = true; | 64 g_has_initialized_best_monitor_color_space = true; |
| 67 } | 65 } |
| 68 | 66 |
| 69 } // namespace gfx | 67 } // namespace gfx |
| OLD | NEW |