blob: 46cb31bc32eeeb23de29b31c09bcb8b5d49d187a [file] [log] [blame]
Avi Drissman3e1a26c2022-09-15 20:26:031// Copyright 2016 The Chromium Authors
ccameronefdab162016-07-25 23:00:022// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ui/gfx/icc_profile.h"
6
7#include <list>
Christopher Cameron374b6c42017-08-31 22:21:238#include <set>
ccameronefdab162016-07-25 23:00:029
ccameron290a91a2017-05-13 19:22:3310#include "base/command_line.h"
cfredric8fdc22432021-10-14 03:24:0011#include "base/containers/lru_cache.h"
ccameronefdab162016-07-25 23:00:0212#include "base/lazy_instance.h"
Hans Wennborg17cd6072020-04-22 15:47:0413#include "base/logging.h"
ccameronefdab162016-07-25 23:00:0214#include "base/synchronization/lock.h"
Mike Kleineb11dca2018-09-05 15:47:0615#include "third_party/skia/include/core/SkColorSpace.h"
Kevin Lubickefd439a2022-07-11 13:50:4516#include "third_party/skia/modules/skcms/skcms.h"
ccameron24c87c32017-03-14 21:50:4217#include "ui/gfx/skia_color_space_util.h"
ccameronefdab162016-07-25 23:00:0218
19namespace gfx {
20
Christopher Cameron374b6c42017-08-31 22:21:2321namespace {
22
Christopher Cameron3bc3d5e2017-11-23 03:48:1123static const size_t kMaxCachedICCProfiles = 16;
24
cfredric8fdc22432021-10-14 03:24:0025// An LRU cache mapping data to ICCProfile objects, to avoid re-parsing
Christopher Cameron3bc3d5e2017-11-23 03:48:1126// profiles every time they are read.
cfredric8fdc22432021-10-14 03:24:0027using DataToProfileCacheBase = base::LRUCache<std::vector<char>, ICCProfile>;
Christopher Cameron3bc3d5e2017-11-23 03:48:1128class DataToProfileCache : public DataToProfileCacheBase {
29 public:
30 DataToProfileCache() : DataToProfileCacheBase(kMaxCachedICCProfiles) {}
31};
Christopher Camerona9b81c0b132017-12-21 00:23:5432base::LazyInstance<DataToProfileCache>::Leaky g_data_to_profile_cache =
33 LAZY_INSTANCE_INITIALIZER;
Christopher Cameron3bc3d5e2017-11-23 03:48:1134
Christopher Cameron20eeb0c2019-04-05 23:40:0035// Lock that must be held to access |g_data_to_profile_cache|.
Daniel Bratellb6c0ab8c2017-12-21 13:09:1836base::LazyInstance<base::Lock>::Leaky g_icc_profile_lock =
37 LAZY_INSTANCE_INITIALIZER;
ccameronefdab162016-07-25 23:00:0238
Christopher Cameron374b6c42017-08-31 22:21:2339} // namespace
40
Brian Osmanf836de52020-03-04 02:13:0141void ICCProfile::Internals::Initialize() {
Christopher Cameronfdc0f872017-11-09 03:22:1642 // Start out with no parametric data.
Christopher Cameron3bc3d5e2017-11-23 03:48:1143 if (data_.empty())
Brian Osmanf836de52020-03-04 02:13:0144 return;
Christopher Camerona4adb41b2017-07-13 06:44:2545
Brian Osman37233e32018-05-17 16:53:1746 // Parse the profile.
47 skcms_ICCProfile profile;
48 if (!skcms_Parse(data_.data(), data_.size(), &profile)) {
49 DLOG(ERROR) << "Failed to parse ICC profile.";
Brian Osmanf836de52020-03-04 02:13:0150 return;
Christopher Camerona4adb41b2017-07-13 06:44:2551 }
Brian Osman37233e32018-05-17 16:53:1752
Brian Osman56adc572018-07-26 17:34:4353 // We have seen many users with profiles that don't have a D50 white point.
54 // Windows appears to detect these profiles, and not use them for OS drawing.
55 // It still returns them when we query the system for the installed profile.
56 // For consistency (and to match old behavior) we reject these profiles on
57 // all platforms.
58 // https://crbug.com/847024
59 const skcms_Matrix3x3& m(profile.toXYZD50);
60 float wX = m.vals[0][0] + m.vals[0][1] + m.vals[0][2];
61 float wY = m.vals[1][0] + m.vals[1][1] + m.vals[1][2];
62 float wZ = m.vals[2][0] + m.vals[2][1] + m.vals[2][2];
Brian Osman56adc572018-07-26 17:34:4363 static const float kD50_WhitePoint[3] = { 0.96420f, 1.00000f, 0.82491f };
64 if (fabsf(wX - kD50_WhitePoint[0]) > 0.04f ||
65 fabsf(wY - kD50_WhitePoint[1]) > 0.04f ||
66 fabsf(wZ - kD50_WhitePoint[2]) > 0.04f) {
Brian Osmanf836de52020-03-04 02:13:0167 return;
Brian Osman56adc572018-07-26 17:34:4368 }
69
Brian Osmanf836de52020-03-04 02:13:0170 // At this point, the profile is considered valid. We still need to determine
71 // if it's representable with a parametric transfer function.
72 is_valid_ = true;
73
Christopher Cameronbadc0022019-04-08 18:37:0274 // Extract the primary matrix, and assume that transfer function is sRGB until
75 // we get something more precise.
76 to_XYZD50_ = profile.toXYZD50;
77 transfer_fn_ = SkNamedTransferFn::kSRGB;
78
Christopher Cameron20eeb0c2019-04-05 23:40:0079 // Coerce it into a rasterization destination (if possible). If the profile
80 // can't be approximated accurately, then use an sRGB transfer function and
81 // return failure. We will continue to use the gamut from this profile.
82 if (!skcms_MakeUsableAsDestinationWithSingleCurve(&profile)) {
Christopher Cameronbadc0022019-04-08 18:37:0283 DLOG(ERROR) << "Parsed ICC profile but can't make usable as destination, "
84 "using sRGB gamma";
Brian Osmanf836de52020-03-04 02:13:0185 return;
Christopher Cameron20eeb0c2019-04-05 23:40:0086 }
Christopher Camerona4adb41b2017-07-13 06:44:2587
Christopher Cameron20eeb0c2019-04-05 23:40:0088 // If SkColorSpace will treat the gamma as that of sRGB, then use the named
89 // constants.
90 sk_sp<SkColorSpace> sk_color_space = SkColorSpace::Make(profile);
Christopher Cameronbadc0022019-04-08 18:37:0291 if (!sk_color_space) {
92 DLOG(ERROR) << "Parsed ICC profile but cannot create SkColorSpace from it, "
93 "using sRGB gamma.";
Brian Osmanf836de52020-03-04 02:13:0194 return;
Christopher Cameron20eeb0c2019-04-05 23:40:0095 }
Brian Osmanf836de52020-03-04 02:13:0196
97 // We were able to get a parametric representation of the transfer function.
98 is_parametric_ = true;
99
Christopher Cameronbadc0022019-04-08 18:37:02100 if (sk_color_space->gammaCloseToSRGB())
Brian Osmanf836de52020-03-04 02:13:01101 return;
ccamerone35e2322017-11-02 23:33:46102
Brian Osman37233e32018-05-17 16:53:17103 // We assume that if we accurately approximated the profile, then the
104 // single-curve version (which may have higher error) is also okay. If we
105 // want to maintain the distinction between accurate and inaccurate profiles,
106 // we could check to see if the single-curve version is/ approximately equal
107 // to the original (or to the multi-channel approximation).
Christopher Cameron20eeb0c2019-04-05 23:40:00108 transfer_fn_ = profile.trc[0].parametric;
Christopher Cameron374b6c42017-08-31 22:21:23109}
Reilly Grantc46032eb2017-08-30 22:53:52110
ccameronefdab162016-07-25 23:00:02111ICCProfile::ICCProfile() = default;
112ICCProfile::ICCProfile(ICCProfile&& other) = default;
113ICCProfile::ICCProfile(const ICCProfile& other) = default;
114ICCProfile& ICCProfile::operator=(ICCProfile&& other) = default;
115ICCProfile& ICCProfile::operator=(const ICCProfile& other) = default;
116ICCProfile::~ICCProfile() = default;
117
118bool ICCProfile::operator==(const ICCProfile& other) const {
Christopher Cameron43f17052017-11-11 01:52:01119 if (!internals_ && !other.internals_)
120 return true;
Christopher Camerondeea2522017-11-16 07:10:52121 if (internals_ && other.internals_) {
Christopher Cameron20eeb0c2019-04-05 23:40:00122 return internals_->data_ == other.internals_->data_;
Christopher Camerondeea2522017-11-16 07:10:52123 }
Christopher Cameron43f17052017-11-11 01:52:01124 return false;
ccameronefdab162016-07-25 23:00:02125}
126
ccameronc21ca23b2017-01-20 03:34:01127bool ICCProfile::operator!=(const ICCProfile& other) const {
128 return !(*this == other);
129}
130
Christopher Cameron43f17052017-11-11 01:52:01131bool ICCProfile::IsValid() const {
132 return internals_ ? internals_->is_valid_ : false;
133}
134
Christopher Cameron3bc3d5e2017-11-23 03:48:11135std::vector<char> ICCProfile::GetData() const {
136 return internals_ ? internals_->data_ : std::vector<char>();
137}
138
ccameronefdab162016-07-25 23:00:02139// static
Christopher Cameron20eeb0c2019-04-05 23:40:00140ICCProfile ICCProfile::FromData(const void* data_as_void, size_t size) {
Christopher Cameron43f17052017-11-11 01:52:01141 const char* data_as_byte = reinterpret_cast<const char*>(data_as_void);
Christopher Cameron3bc3d5e2017-11-23 03:48:11142 std::vector<char> data(data_as_byte, data_as_byte + size);
Reilly Grantc46032eb2017-08-30 22:53:52143
Daniel Bratellb6c0ab8c2017-12-21 13:09:18144 base::AutoLock lock(g_icc_profile_lock.Get());
ccameronefdab162016-07-25 23:00:02145
Christopher Cameron43f17052017-11-11 01:52:01146 // See if there is already an entry with the same data. If so, return that
Christopher Cameron3bc3d5e2017-11-23 03:48:11147 // entry. If not, parse the data.
148 ICCProfile icc_profile;
149 auto found_by_data = g_data_to_profile_cache.Get().Get(data);
150 if (found_by_data != g_data_to_profile_cache.Get().end()) {
151 icc_profile = found_by_data->second;
152 } else {
Christopher Cameron20eeb0c2019-04-05 23:40:00153 icc_profile.internals_ = base::MakeRefCounted<Internals>(std::move(data));
Christopher Cameron43f17052017-11-11 01:52:01154 }
155
Christopher Cameron3bc3d5e2017-11-23 03:48:11156 // Insert the profile into all caches.
Christopher Cameron3bc3d5e2017-11-23 03:48:11157 g_data_to_profile_cache.Get().Put(icc_profile.internals_->data_, icc_profile);
Christopher Camerondeea2522017-11-16 07:10:52158
Christopher Cameron3bc3d5e2017-11-23 03:48:11159 return icc_profile;
ccameronefdab162016-07-25 23:00:02160}
161
Christopher Cameronfdc0f872017-11-09 03:22:16162ColorSpace ICCProfile::GetColorSpace() const {
Zhenyao Mo44c8d1832019-12-10 06:43:10163 if (!internals_ || !internals_->is_valid_)
Christopher Cameron43f17052017-11-11 01:52:01164 return ColorSpace();
Christopher Cameronfdc0f872017-11-09 03:22:16165
Zhenyao Mo44c8d1832019-12-10 06:43:10166 return ColorSpace(ColorSpace::PrimaryID::CUSTOM,
167 ColorSpace::TransferID::CUSTOM, ColorSpace::MatrixID::RGB,
168 ColorSpace::RangeID::FULL, &internals_->to_XYZD50_,
169 &internals_->transfer_fn_);
Christopher Cameron20eeb0c2019-04-05 23:40:00170}
171
Christopher Camerona808f1f2019-05-02 20:29:53172ColorSpace ICCProfile::GetPrimariesOnlyColorSpace() const {
Zhenyao Mo44c8d1832019-12-10 06:43:10173 if (!internals_ || !internals_->is_valid_)
174 return ColorSpace();
175
Christopher Cameron6c9cfff02022-01-25 01:29:03176 return ColorSpace(ColorSpace::PrimaryID::CUSTOM, ColorSpace::TransferID::SRGB,
Zhenyao Mo44c8d1832019-12-10 06:43:10177 ColorSpace::MatrixID::RGB, ColorSpace::RangeID::FULL,
178 &internals_->to_XYZD50_, nullptr);
Christopher Camerona808f1f2019-05-02 20:29:53179}
180
Christopher Cameron20eeb0c2019-04-05 23:40:00181bool ICCProfile::IsColorSpaceAccurate() const {
182 if (!internals_)
183 return false;
184
185 if (!internals_->is_valid_)
186 return false;
187
188 return internals_->is_parametric_;
ccameron24c87c32017-03-14 21:50:42189}
190
Christopher Cameron3bc3d5e2017-11-23 03:48:11191// static
Christopher Cameron20eeb0c2019-04-05 23:40:00192ICCProfile ICCProfile::FromColorSpace(const ColorSpace& color_space) {
Christopher Cameron3bc3d5e2017-11-23 03:48:11193 if (!color_space.IsValid()) {
194 return ICCProfile();
Christopher Cameron43f17052017-11-11 01:52:01195 }
Zhenyao Mo44c8d1832019-12-10 06:43:10196 if (color_space.GetMatrixID() != ColorSpace::MatrixID::RGB) {
Christopher Camerondeea2522017-11-16 07:10:52197 DLOG(ERROR) << "Not creating non-RGB ICCProfile";
Christopher Cameron3bc3d5e2017-11-23 03:48:11198 return ICCProfile();
Christopher Camerondeea2522017-11-16 07:10:52199 }
Zhenyao Mo44c8d1832019-12-10 06:43:10200 if (color_space.GetRangeID() != ColorSpace::RangeID::FULL) {
Christopher Camerondeea2522017-11-16 07:10:52201 DLOG(ERROR) << "Not creating non-full-range ICCProfile";
Christopher Cameron3bc3d5e2017-11-23 03:48:11202 return ICCProfile();
203 }
Brian Osmanb76ea802019-02-01 19:06:23204 skcms_Matrix3x3 to_XYZD50_matrix;
Christopher Cameron3bc3d5e2017-11-23 03:48:11205 color_space.GetPrimaryMatrix(&to_XYZD50_matrix);
Brian Osmanb76ea802019-02-01 19:06:23206 skcms_TransferFunction fn;
Christopher Cameron3bc3d5e2017-11-23 03:48:11207 if (!color_space.GetTransferFunction(&fn)) {
Christopher Camerondeea2522017-11-16 07:10:52208 DLOG(ERROR) << "Failed to get ColorSpace transfer function for ICCProfile.";
Christopher Cameron3bc3d5e2017-11-23 03:48:11209 return ICCProfile();
Christopher Camerondeea2522017-11-16 07:10:52210 }
Brian Osmanb76ea802019-02-01 19:06:23211 sk_sp<SkData> data = SkWriteICCProfile(fn, to_XYZD50_matrix);
Christopher Camerondeea2522017-11-16 07:10:52212 if (!data) {
213 DLOG(ERROR) << "Failed to create SkICC.";
Christopher Cameron3bc3d5e2017-11-23 03:48:11214 return ICCProfile();
Christopher Camerondeea2522017-11-16 07:10:52215 }
Christopher Cameron20eeb0c2019-04-05 23:40:00216 return FromData(data->data(), data->size());
Christopher Cameron3bc3d5e2017-11-23 03:48:11217}
218
Christopher Cameron20eeb0c2019-04-05 23:40:00219ICCProfile::Internals::Internals(std::vector<char> data)
220 : data_(std::move(data)) {
Christopher Camerona4adb41b2017-07-13 06:44:25221 // Parse the ICC profile
Brian Osmanf836de52020-03-04 02:13:01222 Initialize();
Christopher Cameron374b6c42017-08-31 22:21:23223}
224
Christopher Cameron43f17052017-11-11 01:52:01225ICCProfile::Internals::~Internals() {}
226
ccameronefdab162016-07-25 23:00:02227} // namespace gfx