blob: 5702a3b1861c0c00e3fce9b4f80c34fdab600309 [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
Elly Fong-Jonesa8ea66f12024-07-24 18:37:105#ifdef UNSAFE_BUFFERS_BUILD
6// TODO(crbug.com/354829279): Remove this and convert code to safer constructs.
7#pragma allow_unsafe_buffers
8#endif
9
ccameronefdab162016-07-25 23:00:0210#include "ui/gfx/icc_profile.h"
11
Arthur Sonzogni6718b702025-01-09 10:49:1012#include <array>
ccameronefdab162016-07-25 23:00:0213#include <list>
Christopher Cameron374b6c42017-08-31 22:21:2314#include <set>
ccameronefdab162016-07-25 23:00:0215
ccameron290a91a2017-05-13 19:22:3316#include "base/command_line.h"
cfredric8fdc22432021-10-14 03:24:0017#include "base/containers/lru_cache.h"
ccameronefdab162016-07-25 23:00:0218#include "base/lazy_instance.h"
Hans Wennborg17cd6072020-04-22 15:47:0419#include "base/logging.h"
ccameronefdab162016-07-25 23:00:0220#include "base/synchronization/lock.h"
Mike Kleineb11dca2018-09-05 15:47:0621#include "third_party/skia/include/core/SkColorSpace.h"
Kevin Lubick98022952023-02-14 13:48:3422#include "third_party/skia/include/core/SkData.h"
23#include "third_party/skia/include/core/SkRefCnt.h"
Kevin Lubick8b52bc022023-04-25 20:18:0124#include "third_party/skia/include/encode/SkICC.h"
Kevin Lubickefd439a2022-07-11 13:50:4525#include "third_party/skia/modules/skcms/skcms.h"
ccameron24c87c32017-03-14 21:50:4226#include "ui/gfx/skia_color_space_util.h"
ccameronefdab162016-07-25 23:00:0227
28namespace gfx {
29
Christopher Cameron374b6c42017-08-31 22:21:2330namespace {
31
Christopher Cameron3bc3d5e2017-11-23 03:48:1132static const size_t kMaxCachedICCProfiles = 16;
33
cfredric8fdc22432021-10-14 03:24:0034// An LRU cache mapping data to ICCProfile objects, to avoid re-parsing
Christopher Cameron3bc3d5e2017-11-23 03:48:1135// profiles every time they are read.
cfredric8fdc22432021-10-14 03:24:0036using DataToProfileCacheBase = base::LRUCache<std::vector<char>, ICCProfile>;
Christopher Cameron3bc3d5e2017-11-23 03:48:1137class DataToProfileCache : public DataToProfileCacheBase {
38 public:
39 DataToProfileCache() : DataToProfileCacheBase(kMaxCachedICCProfiles) {}
40};
Christopher Camerona9b81c0b132017-12-21 00:23:5441base::LazyInstance<DataToProfileCache>::Leaky g_data_to_profile_cache =
42 LAZY_INSTANCE_INITIALIZER;
Christopher Cameron3bc3d5e2017-11-23 03:48:1143
Christopher Cameron20eeb0c2019-04-05 23:40:0044// Lock that must be held to access |g_data_to_profile_cache|.
Daniel Bratellb6c0ab8c2017-12-21 13:09:1845base::LazyInstance<base::Lock>::Leaky g_icc_profile_lock =
46 LAZY_INSTANCE_INITIALIZER;
ccameronefdab162016-07-25 23:00:0247
Christopher Cameron374b6c42017-08-31 22:21:2348} // namespace
49
Brian Osmanf836de52020-03-04 02:13:0150void ICCProfile::Internals::Initialize() {
Christopher Cameronfdc0f872017-11-09 03:22:1651 // Start out with no parametric data.
Christopher Cameron3bc3d5e2017-11-23 03:48:1152 if (data_.empty())
Brian Osmanf836de52020-03-04 02:13:0153 return;
Christopher Camerona4adb41b2017-07-13 06:44:2554
Brian Osman37233e32018-05-17 16:53:1755 // Parse the profile.
56 skcms_ICCProfile profile;
57 if (!skcms_Parse(data_.data(), data_.size(), &profile)) {
58 DLOG(ERROR) << "Failed to parse ICC profile.";
Brian Osmanf836de52020-03-04 02:13:0159 return;
Christopher Camerona4adb41b2017-07-13 06:44:2560 }
Brian Osman37233e32018-05-17 16:53:1761
Brian Osman56adc572018-07-26 17:34:43