Truly independent web browser
Compositor: Make use of HDR mastering and light level metadata
Zaggy10242aafbb788705
diff --git a/Services/Compositor/CoreAnimationWindowServer.mm b/Services/Compositor/CoreAnimationWindowServer.mmindex 2dedfb64f12..40637f9581d 100644--- a/Services/Compositor/CoreAnimationWindowServer.mm+++ b/Services/Compositor/CoreAnimationWindowServer.mm@@ -5,8 +5,10 @@*/#include <AK/HashTable.h>+#include <AK/Math.h>#include <AK/ScopeGuard.h>#include <AK/StdLibExtras.h>+#include <AK/Vector.h>#include <Compositor/CoreAnimationWindowServer.h>#include <LibCompositing/FrameFitting.h>#include <LibCore/Timer.h>@@ -530,8 +532,40 @@ static CFDataRef create_ambient_viewing_environment()return CFDataCreate(kCFAllocatorDefault, bytes, sizeof(bytes));}-static void enqueue_video_surface(AVSampleBufferDisplayLayer* layer, IOSurfaceRef surface, Media::CodingIndependentCodePoints cicp, Gfx::IntSize shown_size)+template<Unsigned T>+static void append_big_endian(Vector<u8, 24>& bytes, T value){+ for (size_t i = sizeof(T); i > 0; i--)+ bytes.append((value >> ((i - 1) * 8)) & 0xff);+}++// The payload of an HEVC mastering display colour volume SEI message, which lists the primaries as green, blue, red.+static CFDataRef create_mastering_display_color_volume(Media::MasteringDisplayColorVolume const& volume)+{+ constexpr float chromaticity_units_per_unit = 50'000;+ constexpr float luminance_units_per_candela_per_square_metre = 10'000;+ Vector<u8, 24> bytes;+ for (auto const& chromaticity : { volume.green, volume.blue, volume.red, volume.white_point }) {+ append_big_endian(bytes, round_to<u16>(chromaticity.x * chromaticity_units_per_unit));+ append_big_endian(bytes, round_to<u16>(chromaticity.y * chromaticity_units_per_unit));+ }+ append_big_endian(bytes, round_to<u32>(volume.maximum_luminance * luminance_units_per_candela_per_square_metre));+ append_big_endian(bytes, round_to<u32>(volume.minimum_luminance * luminance_units_per_candela_per_square_metre));+ VERIFY(bytes.size() == 24);+ return CFDataCreate(kCFAllocatorDefault, bytes.data(), static_cast<CFIndex>(bytes.size()));+}++static CFDataRef create_content_light_level_information(Media::ContentLightLevelInformation const& information)+{+ Vector<u8, 24> bytes;+ append_big_endian(bytes, information.maximum_content_light_level);+ append_big_endian(bytes, information.maximum_frame_average_light_level);+ return CFDataCreate(kCFAllocatorDefault, bytes.data(), static_cast<CFIndex>(bytes.size()));+}++static void enqueue_video_surface(AVSampleBufferDisplayLayer* layer, IOSurfaceRef surface, Media::ColorMetadata const& color_metadata, Gfx::IntSize shown_size)+{+ auto const& cicp = color_metadata.cicp;CVPixelBufferRef pixel_buffer = nullptr;if (auto result = CVPixelBufferCreateWithIOSurface(kCFAllocatorDefault, surface, nullptr, &pixel_buffer); result != kCVReturnSuccess) {dbgln("Failed to wrap a video surface in a pixel buffer: {}", result);@@ -543,9 +577,22 @@ static void enqueue_video_surface(AVSampleBufferDisplayLayer* layer, IOSurfaceReset_pixel_buffer_attachment(pixel_buffer, kCVImageBufferTransferFunctionKey, CVTransferFunctionGetStringForIntegerCodePoint(to_underlying(cicp.transfer_characteristics())));set_pixel_buffer_attachment(pixel_buffer, kCVImageBufferYCbCrMatrixKey, CVYCbCrMatrixGetStringForIntegerCodePoint(to_underlying(cicp.matrix_coefficients())));if (cicp.transfer_characteristics() == Media::TransferCharacteristics::HLG) {- auto const* ambient_viewing_environment = create_ambient_viewing_environment();- set_pixel_buffer_attachment(pixel_buffer, kCVImageBufferAmbientViewingEnvironmentKey, ambient_viewing_environment);- CFRelease(ambient_viewing_environment);+ if (auto const* ambient_viewing_environment = create_ambient_viewing_environment()) {+ set_pixel_buffer_attachment(pixel_buffer, kCVImageBufferAmbientViewingEnvironmentKey, ambient_viewing_environment);+ CFRelease(ambient_viewing_environment);+ }+ }+ if (auto const& volume = color_metadata.mastering_display_color_volume; volume.has_value()) {+ if (auto const* mastering_display_color_volume = create_mastering_display_color_volume(volume.value())) {+ set_pixel_buffer_attachment(pixel_buffer, kCVImageBufferMasteringDisplayColorVolumeKey, mastering_display_color_volume);+ CFRelease(mastering_display_color_volume);+ }+ }+ if (auto const& information = color_metadata.content_light_level_information; information.has_value() && information->maximum_content_light_level != 0 && information->maximum_frame_average_light_level != 0) {+ if (auto const* content_light_level_information = create_content_light_level_information(information.value())) {+ set_pixel_buffer_attachment(pixel_buffer, kCVImageBufferContentLightLevelInfoKey, content_light_level_information);+ CFRelease(content_light_level_information);+ }}// A decoder's surface can be larger than its picture, and the clean aperture is centred in the surface.@@ -605,7 +652,7 @@ void CoreAnimationRemoteLayer::show_content(ShownSublayer& shown_sublayer, Remotauto visible_size = frame->size().to_type<int>();Gfx::IntSize shown_size { min(visible_size.width(), surface_size.width()), min(visible_size.height(), surface_size.height()) };if (auto* sample_buffer_layer = as_sample_buffer_layer(content_layer)) {- enqueue_video_surface(sample_buffer_layer, surface, frame->cicp(), shown_size);+ enqueue_video_surface(sample_buffer_layer, surface, frame->color_metadata(), shown_size);} else {content_layer.contentsRect = unit_rect_of_surface_part(content_layer, shown_size, surface_size);content_layer.opaque = YES;diff --git a/Services/Compositor/VideoFrameSkia.cpp b/Services/Compositor/VideoFrameSkia.cppindex 2dfd3071451..fa1dac9670a 100644--- a/Services/Compositor/VideoFrameSkia.cpp+++ b/Services/Compositor/VideoFrameSkia.cpp@@ -320,6 +320,30 @@ static sk_sp<SkColorSpace> with_standard_dynamic_range_white_of_perceptual_quantreturn SkColorSpace::MakeRGB(transfer_function, to_xyz_d50);}+static skhdr::Metadata high_dynamic_range_metadata_of(Media::ColorMetadata const& color_metadata)+{+ auto metadata = skhdr::Metadata::MakeEmpty();+ if (auto const& volume = color_metadata.mastering_display_color_volume; volume.has_value()) {+ metadata.setMasteringDisplayColorVolume({+ .fDisplayPrimaries = {+ volume->red.x,+ volume->red.y,+ volume->green.x,+ volume->green.y,+ volume->blue.x,+ volume->blue.y,+ volume->white_point.x,+ volume->white_point.y,+ },+ .fMaximumDisplayMasteringLuminance = volume->maximum_luminance,+ .fMinimumDisplayMasteringLuminance = volume->minimum_luminance,+ });+ }+ if (auto const& information = color_metadata.content_light_level_information; information.has_value() && information->maximum_content_light_level != 0)+ metadata.setContentLightLevelInformation(skhdr::ContentLightLevelInformation::MakeUint16(information->maximum_content_light_level, information->maximum_frame_average_light_level));+ return metadata;+}+// reinterpretColorSpace() copies the pixels of an image in memory, so such an image is given its new colour space by// wrapping the same pixels instead. The pixel data holds a reference to the image, so the pixels stay alive and// unchanged for as long as the new image uses them.@@ -337,7 +361,7 @@ static sk_sp<SkImage> reinterpreted_in_color_space(sk_sp<SkImage> image, sk_sp<S// A PQ or HLG frame holds light up to many times as bright as SDR white, which an SDR surface would clip. Such a frame// is drawn once through a tone map into an sRGB image instead, on the GPU when there is a context for it.-static sk_sp<SkImage> tone_mapped_into_standard_dynamic_range(sk_sp<SkImage> image, GrDirectContext* gr_context)+static sk_sp<SkImage> tone_mapped_into_standard_dynamic_range(Media::ColorMetadata const& color_metadata, sk_sp<SkImage> image, GrDirectContext* gr_context){auto output_color_space = SkColorSpace::MakeSRGB();// Only the tone map sees the image with SDR white moved, so a frame that cannot be tone mapped keeps its own tag.@@ -353,7 +377,7 @@ static sk_sp<SkImage> tone_mapped_into_standard_dynamic_range(sk_sp<SkImage> ima// The headroom is in stops above SDR white.static constexpr float standard_dynamic_range_headroom = 0;- auto tone_map = skhdr::Metadata::MakeEmpty().makeToneMapColorFilter(standard_dynamic_range_headroom, image_to_tone_map->colorSpace());+ auto tone_map = high_dynamic_range_metadata_of(color_metadata).makeToneMapColorFilter(standard_dynamic_range_headroom, image_to_tone_map->colorSpace());if (!tone_map)return image;auto image_info = SkImageInfo::Make(image->width(), image->height(), kBGRA_8888_SkColorType, kPremul_SkAlphaType, output_color_space);@@ -371,7 +395,7 @@ static sk_sp<SkImage> in_standard_dynamic_range(Media::VideoFrame const& frame,{if (!image || !frame_holds_high_dynamic_range(frame))return image;- return tone_mapped_into_standard_dynamic_range(move(image), gr_context);+ return tone_mapped_into_standard_dynamic_range(frame.color_metadata(), move(image), gr_context);}static Gfx::ColorSpace color_space_of(Media::VideoFrame const& frame)@@ -468,7 +492,7 @@ ErrorOr<BitmapInColorSpace> bitmap_from_video_frame(Media::VideoFrame const& fraif (!frame_holds_high_dynamic_range(frame))return BitmapInColorSpace { move(bitmap), color_space_of(frame) };- auto image = tone_mapped_into_standard_dynamic_range(Gfx::sk_image_adopting_bitmap(bitmap, color_space_of(frame)), nullptr);+ auto image = tone_mapped_into_standard_dynamic_range(frame.color_metadata(), Gfx::sk_image_adopting_bitmap(bitmap, color_space_of(frame)), nullptr);auto tone_mapped_bitmap = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, bitmap->size()));auto surface = Gfx::PaintingSurface::wrap_bitmap(*tone_mapped_bitmap);SkPaint paint;