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