OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "ash/display/display_color_manager_chromeos.h" | 5 #include "ash/display/display_color_manager_chromeos.h" |
6 | 6 |
7 #include <stdint.h> | |
8 | |
7 #include <utility> | 9 #include <utility> |
8 | 10 |
9 #include "base/bind.h" | 11 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
11 #include "base/command_line.h" | 13 #include "base/command_line.h" |
12 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
13 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
14 #include "base/format_macros.h" | 16 #include "base/format_macros.h" |
15 #include "base/logging.h" | 17 #include "base/logging.h" |
16 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
17 #include "base/path_service.h" | 19 #include "base/path_service.h" |
18 #include "base/stl_util.h" | 20 #include "base/stl_util.h" |
19 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
20 #include "base/task_runner_util.h" | 22 #include "base/task_runner_util.h" |
21 #include "base/threading/sequenced_worker_pool.h" | 23 #include "base/threading/sequenced_worker_pool.h" |
22 #include "chromeos/chromeos_paths.h" | 24 #include "chromeos/chromeos_paths.h" |
23 #include "third_party/qcms/src/qcms.h" | 25 #include "third_party/qcms/src/qcms.h" |
24 #include "ui/display/types/display_snapshot.h" | 26 #include "ui/display/types/display_snapshot.h" |
25 #include "ui/display/types/gamma_ramp_rgb_entry.h" | 27 #include "ui/display/types/gamma_ramp_rgb_entry.h" |
26 #include "ui/display/types/native_display_delegate.h" | 28 #include "ui/display/types/native_display_delegate.h" |
27 #include "ui/gfx/display.h" | 29 #include "ui/gfx/display.h" |
28 #include "ui/gfx/screen.h" | 30 #include "ui/gfx/screen.h" |
29 | 31 |
30 namespace ash { | 32 namespace ash { |
31 | 33 |
32 namespace { | 34 namespace { |
33 | 35 |
34 bool ParseFile(const base::FilePath& path, | 36 base::FilePath PathForProductID(int64_t product_id) { |
37 base::FilePath path; | |
38 CHECK( | |
39 PathService::Get(chromeos::DIR_DEVICE_COLOR_CALIBRATION_PROFILES, &path)); | |
40 path = path.Append(base::StringPrintf("%08" PRIx64 ".icc", product_id)); | |
41 return path; | |
42 } | |
43 | |
44 bool ParseFile(bool has_color_correction_matrix, | |
45 int64_t product_id, | |
35 DisplayColorManager::ColorCalibrationData* data) { | 46 DisplayColorManager::ColorCalibrationData* data) { |
47 base::FilePath path = PathForProductID(product_id); | |
48 VLOG(1) << "Trying ICC file " << path.value() | |
49 << " with product id: " << product_id | |
50 << " has_color_correction_matrix: " | |
51 << (has_color_correction_matrix ? "true" : "false"); | |
52 | |
36 if (!base::PathExists(path)) // No icc file for this display; not an error. | 53 if (!base::PathExists(path)) // No icc file for this display; not an error. |
37 return false; | 54 return false; |
55 | |
38 qcms_profile* display_profile = qcms_profile_from_path(path.value().c_str()); | 56 qcms_profile* display_profile = qcms_profile_from_path(path.value().c_str()); |
39 | 57 |
40 if (!display_profile) { | 58 if (!display_profile) { |
41 LOG(WARNING) << "Unable to load ICC file: " << path.value(); | 59 LOG(WARNING) << "Unable to load ICC file: " << path.value(); |
42 return false; | 60 return false; |
43 } | 61 } |
44 | 62 |
45 size_t vcgt_channel_length = | 63 size_t vcgt_channel_length = |
46 qcms_profile_get_vcgt_channel_length(display_profile); | 64 qcms_profile_get_vcgt_channel_length(display_profile); |
47 if (!vcgt_channel_length) { | 65 |
66 if (!has_color_correction_matrix && !vcgt_channel_length) { | |
48 LOG(WARNING) << "No vcgt table in ICC file: " << path.value(); | 67 LOG(WARNING) << "No vcgt table in ICC file: " << path.value(); |
49 qcms_profile_release(display_profile); | 68 qcms_profile_release(display_profile); |
50 return false; | 69 return false; |
51 } | 70 } |
52 | 71 |
53 std::vector<uint16_t> vcgt_data; | 72 if (vcgt_channel_length) { |
54 vcgt_data.resize(vcgt_channel_length * 3); | 73 if (has_color_correction_matrix) { |
55 if (!qcms_profile_get_vcgt_rgb_channels(display_profile, &vcgt_data[0])) { | 74 VLOG(1) << "Using VCGT data on CTM enabled platform."; |
56 LOG(WARNING) << "Unable to get vcgt data"; | 75 } |
oshima
2016/03/11 01:35:41
nit: VLOG_IF
| |
57 qcms_profile_release(display_profile); | 76 |
58 return false; | 77 std::vector<uint16_t> vcgt_data; |
78 vcgt_data.resize(vcgt_channel_length * 3); | |
79 if (!qcms_profile_get_vcgt_rgb_channels(display_profile, &vcgt_data[0])) { | |
80 LOG(WARNING) << "Unable to get vcgt data"; | |
81 qcms_profile_release(display_profile); | |
82 return false; | |
83 } | |
84 | |
85 data->gamma_lut.resize(vcgt_channel_length); | |
86 for (size_t i = 0; i < vcgt_channel_length; ++i) { | |
87 data->gamma_lut[i].r = vcgt_data[i]; | |
88 data->gamma_lut[i].g = vcgt_data[vcgt_channel_length + i]; | |
89 data->gamma_lut[i].b = vcgt_data[(vcgt_channel_length * 2) + i]; | |
90 } | |
91 } else { | |
92 VLOG(1) << "Using full degamma/gamma/CTM from profile."; | |
93 qcms_profile* srgb_profile = qcms_profile_sRGB(); | |
94 | |
95 qcms_transform* transform = | |
96 qcms_transform_create(srgb_profile, QCMS_DATA_RGB_8, display_profile, | |
97 QCMS_DATA_RGB_8, QCMS_INTENT_PERCEPTUAL); | |
98 | |
99 if (!transform) { | |
100 LOG(WARNING) | |
101 << "Unable to create transformation from sRGB to display profile."; | |
102 | |
103 qcms_profile_release(display_profile); | |
104 qcms_profile_release(srgb_profile); | |
105 return false; | |
106 } | |
107 | |
108 if (!qcms_transform_is_matrix(transform)) { | |
109 LOG(WARNING) << "No transformation matrix available"; | |
110 | |
111 qcms_transform_release(transform); | |
112 qcms_profile_release(display_profile); | |
113 qcms_profile_release(srgb_profile); | |
114 return false; | |
115 } | |
116 | |
117 size_t degamma_size = qcms_transform_get_input_trc_rgba( | |
118 transform, srgb_profile, QCMS_TRC_USHORT, NULL); | |
119 size_t gamma_size = qcms_transform_get_output_trc_rgba( | |
120 transform, display_profile, QCMS_TRC_USHORT, NULL); | |
121 | |
122 if (degamma_size == 0 || gamma_size == 0) { | |
123 LOG(WARNING) | |
124 << "Invalid number of elements in gamma tables: degamma size = " | |
125 << degamma_size << " gamma size = " << gamma_size; | |
126 | |
127 qcms_transform_release(transform); | |
128 qcms_profile_release(display_profile); | |
129 qcms_profile_release(srgb_profile); | |
130 return false; | |
131 } | |
132 | |
133 std::vector<uint16_t> degamma_data; | |
134 std::vector<uint16_t> gamma_data; | |
135 degamma_data.resize(degamma_size * 4); | |
136 gamma_data.resize(gamma_size * 4); | |
137 | |
138 qcms_transform_get_input_trc_rgba(transform, srgb_profile, QCMS_TRC_USHORT, | |
139 °amma_data[0]); | |
140 qcms_transform_get_output_trc_rgba(transform, display_profile, | |
141 QCMS_TRC_USHORT, &gamma_data[0]); | |
142 | |
143 data->degamma_lut.resize(degamma_size); | |
144 for (size_t i = 0; i < degamma_size; ++i) { | |
145 data->degamma_lut[i].r = degamma_data[i * 4]; | |
146 data->degamma_lut[i].g = degamma_data[(i * 4) + 1]; | |
147 data->degamma_lut[i].b = degamma_data[(i * 4) + 2]; | |
148 } | |
149 | |
150 data->gamma_lut.resize(gamma_size); | |
151 for (size_t i = 0; i < gamma_size; ++i) { | |
152 data->gamma_lut[i].r = gamma_data[i * 4]; | |
153 data->gamma_lut[i].g = gamma_data[(i * 4) + 1]; | |
154 data->gamma_lut[i].b = gamma_data[(i * 4) + 2]; | |
155 } | |
156 | |
157 data->correction_matrix.resize(9); | |
158 for (int i = 0; i < 9; ++i) { | |
159 data->correction_matrix[i] = | |
160 qcms_transform_get_matrix(transform, i / 3, i % 3); | |
161 } | |
162 | |
163 qcms_transform_release(transform); | |
164 qcms_profile_release(srgb_profile); | |
59 } | 165 } |
60 | 166 |
61 data->lut.resize(vcgt_channel_length); | 167 VLOG(1) << "ICC file successfully parsed"; |
62 for (size_t i = 0; i < vcgt_channel_length; ++i) { | |
63 data->lut[i].r = vcgt_data[i]; | |
64 data->lut[i].g = vcgt_data[vcgt_channel_length + i]; | |
65 data->lut[i].b = vcgt_data[(vcgt_channel_length * 2) + i]; | |
66 } | |
67 qcms_profile_release(display_profile); | 168 qcms_profile_release(display_profile); |
68 return true; | 169 return true; |
69 } | 170 } |
70 | 171 |
71 base::FilePath PathForDisplaySnapshot(const ui::DisplaySnapshot* snapshot) { | |
72 base::FilePath path; | |
73 CHECK( | |
74 PathService::Get(chromeos::DIR_DEVICE_COLOR_CALIBRATION_PROFILES, &path)); | |
75 path = path.Append( | |
76 base::StringPrintf("%08" PRIx64 ".icc", snapshot->product_id())); | |
77 return path; | |
78 } | |
79 | |
80 } // namespace | 172 } // namespace |
81 | 173 |
82 DisplayColorManager::DisplayColorManager( | 174 DisplayColorManager::DisplayColorManager( |
83 ui::DisplayConfigurator* configurator, | 175 ui::DisplayConfigurator* configurator, |
84 base::SequencedWorkerPool* blocking_pool) | 176 base::SequencedWorkerPool* blocking_pool) |
85 : configurator_(configurator), blocking_pool_(blocking_pool) { | 177 : configurator_(configurator), blocking_pool_(blocking_pool) { |
86 configurator_->AddObserver(this); | 178 configurator_->AddObserver(this); |
87 } | 179 } |
88 | 180 |
89 DisplayColorManager::~DisplayColorManager() { | 181 DisplayColorManager::~DisplayColorManager() { |
90 configurator_->RemoveObserver(this); | 182 configurator_->RemoveObserver(this); |
91 STLDeleteValues(&calibration_map_); | 183 STLDeleteValues(&calibration_map_); |
92 } | 184 } |
93 | 185 |
94 void DisplayColorManager::OnDisplayModeChanged( | 186 void DisplayColorManager::OnDisplayModeChanged( |
95 const ui::DisplayConfigurator::DisplayStateList& display_states) { | 187 const ui::DisplayConfigurator::DisplayStateList& display_states) { |
96 for (const ui::DisplaySnapshot* state : display_states) { | 188 for (const ui::DisplaySnapshot* state : display_states) { |
97 if (calibration_map_[state->product_id()]) { | 189 if (calibration_map_[state->product_id()]) { |
98 ApplyDisplayColorCalibration(state->display_id(), state->product_id()); | 190 ApplyDisplayColorCalibration(state->display_id(), state->product_id()); |
99 } else { | 191 } else { |
100 if (state->product_id() != ui::DisplaySnapshot::kInvalidProductID) | 192 if (state->product_id() != ui::DisplaySnapshot::kInvalidProductID) |
101 LoadCalibrationForDisplay(state); | 193 LoadCalibrationForDisplay(state); |
102 } | 194 } |
103 } | 195 } |
104 } | 196 } |
105 | 197 |
106 void DisplayColorManager::ApplyDisplayColorCalibration(int64_t display_id, | 198 void DisplayColorManager::ApplyDisplayColorCalibration(int64_t display_id, |
107 int64_t product_id) { | 199 int64_t product_id) { |
108 if (calibration_map_.find(product_id) != calibration_map_.end()) { | 200 if (calibration_map_.find(product_id) != calibration_map_.end()) { |
109 ColorCalibrationData* ramp = calibration_map_[product_id]; | 201 ColorCalibrationData* data = calibration_map_[product_id]; |
110 if (!configurator_->SetGammaRamp(display_id, ramp->lut)) | 202 if (!configurator_->SetColorCorrection(display_id, data->degamma_lut, |
111 LOG(WARNING) << "Error applying gamma ramp"; | 203 data->gamma_lut, |
204 data->correction_matrix)) | |
205 LOG(WARNING) << "Error applying color correction data"; | |
112 } | 206 } |
113 } | 207 } |
114 | 208 |
115 void DisplayColorManager::LoadCalibrationForDisplay( | 209 void DisplayColorManager::LoadCalibrationForDisplay( |
116 const ui::DisplaySnapshot* display) { | 210 const ui::DisplaySnapshot* display) { |
117 if (display->display_id() == gfx::Display::kInvalidDisplayID) { | 211 if (display->display_id() == gfx::Display::kInvalidDisplayID) { |
118 LOG(WARNING) << "Trying to load calibration data for invalid display id"; | 212 LOG(WARNING) << "Trying to load calibration data for invalid display id"; |
119 return; | 213 return; |
120 } | 214 } |
121 | 215 |
122 base::FilePath path = PathForDisplaySnapshot(display); | 216 scoped_ptr<ColorCalibrationData> data(new ColorCalibrationData()); |
123 VLOG(1) << "Loading ICC file " << path.value() | 217 base::Callback<bool(void)> request; |
124 << " for display id: " << display->display_id() | 218 request = base::Bind(&ParseFile, display->has_color_correction_matrix(), |
125 << " with product id: " << display->product_id(); | 219 display->product_id(), base::Unretained(data.get())); |
126 | 220 |
127 scoped_ptr<ColorCalibrationData> data(new ColorCalibrationData()); | |
128 base::Callback<bool(void)> request( | |
129 base::Bind(&ParseFile, path, base::Unretained(data.get()))); | |
130 base::PostTaskAndReplyWithResult( | 221 base::PostTaskAndReplyWithResult( |
131 blocking_pool_, FROM_HERE, request, | 222 blocking_pool_, FROM_HERE, request, |
132 base::Bind(&DisplayColorManager::UpdateCalibrationData, AsWeakPtr(), | 223 base::Bind(&DisplayColorManager::UpdateCalibrationData, AsWeakPtr(), |
133 display->display_id(), display->product_id(), | 224 display->display_id(), display->product_id(), |
134 base::Passed(std::move(data)))); | 225 base::Passed(std::move(data)))); |
135 } | 226 } |
136 | 227 |
137 void DisplayColorManager::UpdateCalibrationData( | 228 void DisplayColorManager::UpdateCalibrationData( |
138 int64_t display_id, | 229 int64_t display_id, |
139 int64_t product_id, | 230 int64_t product_id, |
140 scoped_ptr<ColorCalibrationData> data, | 231 scoped_ptr<ColorCalibrationData> data, |
141 bool success) { | 232 bool success) { |
142 DCHECK_EQ(base::MessageLoop::current()->type(), base::MessageLoop::TYPE_UI); | 233 DCHECK_EQ(base::MessageLoop::current()->type(), base::MessageLoop::TYPE_UI); |
143 if (success) { | 234 if (success) { |
144 // The map takes over ownership of the underlying memory. | 235 // The map takes over ownership of the underlying memory. |
145 calibration_map_[product_id] = data.release(); | 236 calibration_map_[product_id] = data.release(); |
146 ApplyDisplayColorCalibration(display_id, product_id); | 237 ApplyDisplayColorCalibration(display_id, product_id); |
147 } | 238 } |
148 } | 239 } |
149 | 240 |
150 DisplayColorManager::ColorCalibrationData::ColorCalibrationData() {} | 241 DisplayColorManager::ColorCalibrationData::ColorCalibrationData() {} |
151 | 242 |
152 DisplayColorManager::ColorCalibrationData::~ColorCalibrationData() {} | 243 DisplayColorManager::ColorCalibrationData::~ColorCalibrationData() {} |
153 | 244 |
154 } // namespace ash | 245 } // namespace ash |
OLD | NEW |