Avi Drissman | 3e1a26c | 2022-09-15 20:26:03 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors |
ccameron | efdab16 | 2016-07-25 23:00:02 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Elly Fong-Jones | a8ea66f1 | 2024-07-24 18:37:10 | [diff] [blame] | 5 | #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 | |
ccameron | efdab16 | 2016-07-25 23:00:02 | [diff] [blame] | 10 | #include "ui/gfx/icc_profile.h" |
| 11 | |
Arthur Sonzogni | 6718b70 | 2025-01-09 10:49:10 | [diff] [blame] | 12 | #include <array> |
ccameron | efdab16 | 2016-07-25 23:00:02 | [diff] [blame] | 13 | #include <list> |
Christopher Cameron | 374b6c4 | 2017-08-31 22:21:23 | [diff] [blame] | 14 | #include <set> |
ccameron | efdab16 | 2016-07-25 23:00:02 | [diff] [blame] | 15 | |
ccameron | 290a91a | 2017-05-13 19:22:33 | [diff] [blame] | 16 | #include "base/command_line.h" |
cfredric | 8fdc2243 | 2021-10-14 03:24:00 | [diff] [blame] | 17 | #include "base/containers/lru_cache.h" |
ccameron | efdab16 | 2016-07-25 23:00:02 | [diff] [blame] | 18 | #include "base/lazy_instance.h" |
Hans Wennborg | 17cd607 | 2020-04-22 15:47:04 | [diff] [blame] | 19 | #include "base/logging.h" |
ccameron | efdab16 | 2016-07-25 23:00:02 | [diff] [blame] | 20 | #include "base/synchronization/lock.h" |
Mike Klein | eb11dca | 2018-09-05 15:47:06 | [diff] [blame] | 21 | #include "third_party/skia/include/core/SkColorSpace.h" |
Kevin Lubick | 9802295 | 2023-02-14 13:48:34 | [diff] [blame] | 22 | #include "third_party/skia/include/core/SkData.h" |
| 23 | #include "third_party/skia/include/core/SkRefCnt.h" |
Kevin Lubick | 8b52bc02 | 2023-04-25 20:18:01 | [diff] [blame] | 24 | #include "third_party/skia/include/encode/SkICC.h" |
Kevin Lubick | efd439a | 2022-07-11 13:50:45 | [diff] [blame] | 25 | #include "third_party/skia/modules/skcms/skcms.h" |
ccameron | 24c87c3 | 2017-03-14 21:50:42 | [diff] [blame] | 26 | #include "ui/gfx/skia_color_space_util.h" |
ccameron | efdab16 | 2016-07-25 23:00:02 | [diff] [blame] | 27 | |
| 28 | namespace gfx { |
| 29 | |
Christopher Cameron | 374b6c4 | 2017-08-31 22:21:23 | [diff] [blame] | 30 | namespace { |
| 31 | |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame] | 32 | static const size_t kMaxCachedICCProfiles = 16; |
| 33 | |
cfredric | 8fdc2243 | 2021-10-14 03:24:00 | [diff] [blame] | 34 | // An LRU cache mapping data to ICCProfile objects, to avoid re-parsing |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame] | 35 | // profiles every time they are read. |
cfredric | 8fdc2243 | 2021-10-14 03:24:00 | [diff] [blame] | 36 | using DataToProfileCacheBase = base::LRUCache<std::vector<char>, ICCProfile>; |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame] | 37 | class DataToProfileCache : public DataToProfileCacheBase { |
| 38 | public: |
| 39 | DataToProfileCache() : DataToProfileCacheBase(kMaxCachedICCProfiles) {} |
| 40 | }; |
Christopher Cameron | a9b81c0b13 | 2017-12-21 00:23:54 | [diff] [blame] | 41 | base::LazyInstance<DataToProfileCache>::Leaky g_data_to_profile_cache = |
| 42 | LAZY_INSTANCE_INITIALIZER; |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame] | 43 | |
Christopher Cameron | 20eeb0c | 2019-04-05 23:40:00 | [diff] [blame] | 44 | // Lock that must be held to access |g_data_to_profile_cache|. |
Daniel Bratell | b6c0ab8c | 2017-12-21 13:09:18 | [diff] [blame] | 45 | base::LazyInstance<base::Lock>::Leaky g_icc_profile_lock = |
| 46 | LAZY_INSTANCE_INITIALIZER; |
ccameron | efdab16 | 2016-07-25 23:00:02 | [diff] [blame] | 47 | |
Christopher Cameron | 374b6c4 | 2017-08-31 22:21:23 | [diff] [blame] | 48 | } // namespace |
| 49 | |
Brian Osman | f836de5 | 2020-03-04 02:13:01 | [diff] [blame] | 50 | void ICCProfile::Internals::Initialize() { |
Christopher Cameron | fdc0f87 | 2017-11-09 03:22:16 | [diff] [blame] | 51 | // Start out with no parametric data. |
Christopher Cameron | 3bc3d5e | 2017-11-23 03:48:11 | [diff] [blame] | 52 | if (data_.empty()) |
Brian Osman | f836de5 | 2020-03-04 02:13:01 | [diff] [blame] | 53 | return; |
Christopher Cameron | a4adb41b | 2017-07-13 06:44:25 | [diff] [blame] | 54 | |
Brian Osman | 37233e3 | 2018-05-17 16:53:17 | [diff] [blame] | 55 | // 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 Osman | f836de5 | 2020-03-04 02:13:01 | [diff] [blame] | 59 | return; |
Christopher Cameron | a4adb41b | 2017-07-13 06:44:25 | [diff] [blame] | 60 | } |
Brian Osman | 37233e3 | 2018-05-17 16:53:17 | [diff] [blame] | 61 | |
Brian Osman | 56adc57 | 2018-07-26 17:34:43 | [diff] [
|