Skip to content

Truly independent web browser

LibGfx+Compositor: Parse ICC profiles in the compositor

Aliaksandr Kalenikbd63e21c1a0e
diff --git a/Libraries/LibGfx/CMakeLists.txt b/Libraries/LibGfx/CMakeLists.txt
index eea9a53e5f9..fcea4618013 100644
--- a/Libraries/LibGfx/CMakeLists.txt
+++ b/Libraries/LibGfx/CMakeLists.txt
@@ -6,7 +6,6 @@ set(SOURCES
Color.cpp
ColorConversion.cpp
ColorSpace.cpp
- ColorSpaceSkia.cpp
CornerRadii.cpp
Cursor.cpp
Filter.cpp
diff --git a/Libraries/LibGfx/ColorSpace.cpp b/Libraries/LibGfx/ColorSpace.cpp
index 9879c77e802..6a53ac0ab65 100644
--- a/Libraries/LibGfx/ColorSpace.cpp
+++ b/Libraries/LibGfx/ColorSpace.cpp
@@ -4,7 +4,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-#include <AK/AllOf.h>
#include <LibGfx/ColorSpace.h>
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
@@ -65,6 +64,13 @@ ErrorOr<ColorSpace> ColorSpace::from_cicp(Media::CodingIndependentCodePoints cic
return ColorSpace { CICP { cicp.color_primaries(), cicp.transfer_characteristics() } };
}
+ErrorOr<ColorSpace> ColorSpace::from_icc_bytes(ReadonlyBytes icc_bytes)
+{
+ if (icc_bytes.is_empty())
+ return ColorSpace {};
+ return ColorSpace { ICCProfile::create(TRY(ByteBuffer::copy(icc_bytes))) };
+}
+
}
namespace IPC {
@@ -72,7 +78,7 @@ namespace IPC {
enum class ColorSpaceKind : u8 {
SRGB,
CICP,
- Parametric,
+ ICCProfile,
};
template<>
@@ -88,12 +94,9 @@ ErrorOr<void> encode(Encoder& encoder, Gfx::ColorSpace const& color_space)
TRY(encoder.encode(to_underlying(cicp.transfer_characteristics)));
return {};
},
- [&](Gfx::ColorSpace::Parametric const& parametric) -> ErrorOr<void> {
- TRY(encoder.encode(ColorSpaceKind::Parametric));
- for (auto value : parametric.transfer_function)
- TRY(encoder.encode(value));
- for (auto value : parametric.to_xyz_d50)
- TRY(encoder.encode(value));
+ [&](NonnullRefPtr<Gfx::ColorSpace::ICCProfile const> const& icc_profile) -> ErrorOr<void> {
+ TRY(encoder.encode(ColorSpaceKind::ICCProfile));
+ TRY(encoder.encode(icc_profile->bytes()));
return {};
});
}
@@ -112,16 +115,11 @@ ErrorOr<Gfx::ColorSpace> decode(Decoder& decoder)
TRY(Gfx::check_transfer_characteristics(transfer_characteristics));
return Gfx::ColorSpace { Gfx::ColorSpace::CICP { color_primaries, transfer_characteristics } };
}
- case ColorSpaceKind::Parametric: {
- Gfx::ColorSpace::Parametric parametric;
- for (auto& value : parametric.transfer_function)
- value = TRY(decoder.decode<float>());
- for (auto& value : parametric.to_xyz_d50)
- value = TRY(decoder.decode<float>());
- auto is_finite = [](float value) { return __builtin_isfinite(value); };
- if (!all_of(parametric.transfer_function, is_finite) || !all_of(parametric.to_xyz_d50, is_finite))
- return Error::from_string_literal("IPC: Color space has a value that is not finite");
- return Gfx::ColorSpace { move(parametric) };
+ case ColorSpaceKind::ICCProfile: {
+ auto icc_bytes = TRY(decoder.decode<ByteBuffer>());
+ if (icc_bytes.is_empty())
+ return Error::from_string_literal("IPC: ICC profile has no bytes");
+ return Gfx::ColorSpace { Gfx::ColorSpace::ICCProfile::create(move(icc_bytes)) };
}
}
return Error::from_string_literal("IPC: Unknown color space kind");
diff --git a/Libraries/LibGfx/ColorSpace.h b/Libraries/LibGfx/ColorSpace.h
index 5dc246c2a7a..0da0eaa99db 100644
--- a/Libraries/LibGfx/ColorSpace.h
+++ b/Libraries/LibGfx/ColorSpace.h
@@ -6,15 +6,17 @@
#pragma once
-#include <AK/Array.h>
+#include <AK/AtomicRefCounted.h>
+#include <AK/ByteBuffer.h>
#include <AK/Error.h>
+#include <AK/NonnullRefPtr.h>
#include <AK/Variant.h>
#include <LibIPC/Forward.h>
#include <LibMediaCodecs/Color/CodingIndependentCodePoints.h>
namespace Gfx {
-// The color space of an image, as plain data. The painter builds its own color space object from it when it draws.
+// The color space of an image, as data. The compositor builds its own color space object from it when it draws.
class ColorSpace {
public:
// The parts of a coding-independent code point that set the color space of RGB pixels.
@@ -23,20 +25,30 @@ public:
Media::TransferCharacteristics transfer_characteristics { Media::TransferCharacteristics::Unspecified };
};
- // A transfer function with the parameters g, a, b, c, d, e and f, and a row-major matrix from linear RGB to XYZ
- // with a D50 white point.
- struct Parametric {
- Array<float, 7> transfer_function {};
- Array<float, 9> to_xyz_d50 {};
+ // The bytes of an ICC profile, as the image carries them. Only the compositor parses them, so that no other
+ // process needs an ICC parser. A profile that it cannot parse gives sRGB.
+ class ICCProfile final : public AtomicRefCounted<ICCProfile> {
+ public:
+ static NonnullRefPtr<ICCProfile const> create(ByteBuffer bytes) { return adopt_ref(*new ICCProfile(move(bytes))); }
+
+ ReadonlyBytes bytes() const { return m_bytes; }
+
+ private:
+ explicit ICCProfile(ByteBuffer bytes)
+ : m_bytes(move(bytes))
+ {
+ }
+
+ ByteBuffer m_bytes;
};
// Without data, the color space is sRGB.
- using Data = Variant<Empty, CICP, Parametric>;
+ using Data = Variant<Empty, CICP, NonnullRefPtr<ICCProfile const>>;
ColorSpace() = default;
static ErrorOr<ColorSpace> from_cicp(Media::CodingIndependentCodePoints);
- static ErrorOr<ColorSpace> load_from_icc_bytes(ReadonlyBytes);
+ static ErrorOr<ColorSpace> from_icc_bytes(ReadonlyBytes);
Data const& data() const { return m_data; }
diff --git a/Libraries/LibGfx/ColorSpaceSkia.cpp b/Libraries/LibGfx/ColorSpaceSkia.cpp
deleted file mode 100644
index 70ae8d436f0..00000000000
--- a/Libraries/LibGfx/ColorSpaceSkia.cpp
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright (c) 2024, Lucas Chollet <[email protected]>
- *
- * SPDX-License-Identifier: BSD-2-Clause
- */
-
-#include <LibGfx/ColorSpace.h>
-
-#include <core/SkColorSpace.h>
-
-namespace Gfx {
-
-ErrorOr<ColorSpace> ColorSpace::load_from_icc_bytes(ReadonlyBytes icc_bytes)
-{
- if (icc_bytes.is_empty())
- return ColorSpace {};
-
- skcms_ICCProfile icc_profile {};
- if (!skcms_Parse(icc_bytes.data(), icc_bytes.size(), &icc_profile))
- return Error::from_string_literal("Failed to parse the ICC profile");
-
- auto color_space = SkColorSpace::Make(icc_profile);
- if (!color_space && icc_profile.has_trc && icc_profile.has_toXYZD50) {
- skcms_TransferFunction transfer_function;
- float max_error;
- if (skcms_ApproximateCurve(&icc_profile.trc[0], &transfer_function, &max_error))
- color_space = SkColorSpace::MakeRGB(transfer_function, icc_profile.toXYZD50);
- }
- if (!color_space)
- return ColorSpace {};
-
- // A Skia color space is exactly a transfer function and a matrix to XYZ D50, so this keeps all of it.
- skcms_TransferFunction transfer_function;
- color_space->transferFn(&transfer_function);
- skcms_Matrix3x3 to_xyz_d50;
- VERIFY(color_space->toXYZD50(&to_xyz_d50));
-
- Parametric parametric;
- parametric.transfer_function = { transfer_function.g, transfer_function.a, transfer_function.b, transfer_function.c, transfer_function.d, transfer_function.e, transfer_function.f };
- for (size_t row = 0; row < 3; ++row) {
- for (size_t column = 0; column < 3; ++column)
- parametric.to_xyz_d50[row * 3 + column] = to_xyz_d50.vals[row][column];
- }
- return ColorSpace { move(parametric) };
-}
-
-}
diff --git a/Libraries/LibGfx/ImageFormats/ImageDecoder.cpp b/Libraries/LibGfx/ImageFormats/ImageDecoder.cpp
index fbae5ff9a5f..9ff92c6ba5c 100644
--- a/Libraries/LibGfx/ImageFormats/ImageDecoder.cpp
+++ b/Libraries/LibGfx/ImageFormats/ImageDecoder.cpp
@@ -61,7 +61,7 @@ ErrorOr<ColorSpace> ImageDecoder::color_space()
auto maybe_icc_data = TRY(icc_data());
if (!maybe_icc_data.has_value())
return ColorSpace {};
- return ColorSpace::load_from_icc_bytes(maybe_icc_data.value());
+ return ColorSpace::from_icc_bytes(maybe_icc_data.value());
}
ErrorOr<RefPtr<ImageDecoder>> ImageDecoder::try_create_for_raw_bytes(ReadonlyBytes bytes, [[maybe_unused]] Optional<ByteString> mime_type)
diff --git a/Services/Compositor/ColorSpaceSkia.cpp b/Services/Compositor/ColorSpaceSkia.cpp
index 5d61c29bb7f..dcd57a019ab 100644
--- a/Services/Compositor/ColorSpaceSkia.cpp
+++ b/Services/Compositor/ColorSpaceSkia.cpp
@@ -101,19 +101,26 @@ static sk_sp<SkColorSpace> create_skia_color_space(ColorSpace::Data const& data)
[](ColorSpace::CICP const& cicp) -> sk_sp<SkColorSpace> {
return SkColorSpace::MakeRGB(transfer_function_for_transfer_characteristics(cicp.transfer_characteristics), to_xyz_d50_for_color_primaries(cicp.color_primaries));
},
- [](ColorSpace::Parametric const& parametric) -> sk_sp<SkColorSpace> {
- auto const& values = parametric.transfer_function;
- skcms_TransferFunction transfer_function { values[0], values[1], values[2], values[3], values[4], values[5], values[6] };
- skcms_Matrix3x3 to_xyz_d50;
- for (size_t row = 0; row < 3; ++row) {
- for (size_t column = 0; column < 3; ++column)
- to_xyz_d50.vals[row][column] = parametric.to_xyz_d50[row * 3 + column];
+ [](NonnullRefPtr<ColorSpace::ICCProfile const> const& icc_profile) -> sk_sp<SkColorSpace> {
+ auto icc_bytes = icc_profile->bytes();
+ skcms_ICCProfile parsed_icc_profile {};
+ if (!skcms_Parse(icc_bytes.data(), icc_bytes.size(), &parsed_icc_profile)) {
+ dbgln("Failed to parse an ICC profile, drawing as sRGB");
+ return nullptr;
}
- return SkColorSpace::MakeRGB(transfer_function, to_xyz_d50);
+
+ auto color_space = SkColorSpace::Make(parsed_icc_profile);
+ if (!color_space && parsed_icc_profile.has_trc && parsed_icc_profile.has_toXYZD50) {
+ skcms_TransferFunction transfer_function;
+ float max_error;
+ if (skcms_ApproximateCurve(&parsed_icc_profile.trc[0], &transfer_function, &max_error))
+ color_space = SkColorSpace::MakeRGB(transfer_function, parsed_icc_profile.toXYZD50);
+ }
+ return color_space;
});
}
-static bool have_same_bits(ColorSpace::Data const& a, ColorSpace::Data const& b)
+static bool have_same_data(ColorSpace::Data const& a, ColorSpace::Data const& b)
{
if (a.index() != b.index())
return false;
@@ -123,10 +130,9 @@ static bool have_same_bits(ColorSpace::Data const& a, ColorSpace::Data const& b)
auto const& other = b.get<ColorSpace::CICP>();
return cicp.color_primaries == other.color_primaries && cicp.transfer_characteristics == other.transfer_characteristics;
},
- [&](ColorSpace::Parametric const& parametric) {
- auto const& other = b.get<ColorSpace::Parametric>();
- return __builtin_memcmp(parametric.transfer_function.data(), other.transfer_function.data(), sizeof(parametric.transfer_function)) == 0
- && __builtin_memcmp(parametric.to_xyz_d50.data(), other.to_xyz_d50.data(), sizeof(parametric.to_xyz_d50)) == 0;
+ [&](NonnullRefPtr<ColorSpace::ICCProfile const> const& icc_profile) {
+ auto const& other = b.get<NonnullRefPtr<ColorSpace::ICCProfile const>>();
+ return icc_profile->bytes() == other->bytes();
});
}
@@ -135,8 +141,8 @@ sk_sp<SkColorSpace> to_skia_color_space(ColorSpace const& color_space)
if (color_space.data().has<Empty>())
return nullptr;
- // Images in one color space are drawn many times, and Skia makes a new color space object for each request that
- // is not sRGB. So keep the color spaces that were made last.
+ // Images in one color space are drawn many times, and each request would parse the ICC profile again, or make a
+ // new Skia color space object for anything but sRGB. So keep the color spaces that were made last.
struct CachedColorSpace {
ColorSpace::Data data;
sk_sp<SkColorSpace> skia_color_space;
@@ -150,7 +156,7 @@ sk_sp<SkColorSpace> to_skia_color_space(ColorSpace const& color_space)
MutexLocker locker(cache->mutex);
for (auto const& entry : cache->entries) {
- if (have_same_bits(entry.data, color_space.data()))
+ if (have_same_data(entry.data, color_space.data()))
return entry.skia_color_space;
}
if (cache->entries.size() >= max_cached_color_spaces)