Truly independent web browser
Compositor: Show HDR video in extended dynamic range
Aliaksandr Kalenik3869da3fb056
diff --git a/Services/Compositor/ContextPresenter.cpp b/Services/Compositor/ContextPresenter.cppindex 4650eb0a012..bef5b582086 100644--- a/Services/Compositor/ContextPresenter.cpp+++ b/Services/Compositor/ContextPresenter.cpp@@ -4,6 +4,7 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/AnyOf.h>#include <AK/Math.h>#include <Compositor/CanvasSurfaceRegistry.h>#include <Compositor/ContextPresenter.h>@@ -42,6 +43,12 @@ static Gfx::FloatSize bounded_composited_raster_scale(Gfx::FloatSize scale, Gfx:return scale;}+static bool video_frame_holds_high_dynamic_range(Media::VideoFrame const& frame)+{+ auto transfer_characteristics = frame.cicp().transfer_characteristics();+ return transfer_characteristics == Media::TransferCharacteristics::SMPTE2084 || transfer_characteristics == Media::TransferCharacteristics::HLG;+}+ContextPresenter::ContextPresenter(OwnedCanvasSurfaces canvas_surfaces, bool presents_to_client): m_canvas_surfaces(canvas_surfaces), m_presents_to_client(presents_to_client)@@ -651,6 +658,19 @@ ContextPresenter::LayerUpdate ContextPresenter::layer_update_for(FrameInputs conif (!presents_to_client())return layers;layers.active_media = m_media_activity.active_media(now);+ // An HDR video is a layer from its first frame for as long as it is drawn, even while paused, so that the window+ // server keeps showing it brighter than SDR white instead of it going back into the frame, which holds SDR.+ if (m_window_server_may_show_media) {+ for (auto id : m_display_list->referenced_resource_ids().video_sinks) {+ LiveMedia media { RustFFI::FfiLiveMediaKind::Video, id };+ auto is_active = any_of(layers.active_media, [&](auto const& active) { return active.kind == media.kind && active.id == media.id; });+ if (is_active)+ continue;+ auto frame = current_video_frame(Compositing::VideoSinkResourceId { id });+ if (frame && video_frame_holds_high_dynamic_range(*frame))+ layers.active_media.append(media);+ }+ }if (layers.active_media.is_empty())return layers;auto plan_holds_for_frame = m_layer_plan@@ -879,10 +899,6 @@ bool ContextPresenter::window_server_can_show_media_layers_of(LayerPlan const& pauto frame = current_video_frame(Compositing::VideoSinkResourceId { layer.media.id });if (!frame || !frame->surface())return false;- // The window server would show PQ and HLG video as SDR video that it is not, so the frame keeps drawing it.- auto transfer_characteristics = frame->cicp().transfer_characteristics();- if (transfer_characteristics == Media::TransferCharacteristics::SMPTE2084 || transfer_characteristics == Media::TransferCharacteristics::HLG)- return false;}}return true;diff --git a/Services/Compositor/RemoteLayer.mm b/Services/Compositor/RemoteLayer.mmindex 65a8113c394..485cc006070 100644--- a/Services/Compositor/RemoteLayer.mm+++ b/Services/Compositor/RemoteLayer.mm@@ -339,22 +339,65 @@ void RemoteLayer::update_shown_sublayer(ShownSublayer& shown_sublayer, RemoteSubshow_content(shown_sublayer, sublayer);}+// Without metadata saying otherwise, HDR video is taken to peak at 1000 nits, with SDR white at the 203 nits of+// ITU-R BT.2408.+static constexpr float assumed_headroom_of_high_dynamic_range_video = 1000.0f / 203.0f;++static bool holds_high_dynamic_range(Media::CodingIndependentCodePoints cicp)+{+ return cicp.transfer_characteristics() == Media::TransferCharacteristics::SMPTE2084 || cicp.transfer_characteristics() == Media::TransferCharacteristics::HLG;+}+// The window server converts a YCbCr surface to RGB by the matrix it is tagged with. It decodes the transfer of SDR// video by a curve of its own whatever the transfer is tagged as, but the colour space of the surface overrides that,-// and naming it after sRGB draws the video as the frame draws it.+// and naming it after sRGB draws the video as the frame draws it. PQ and HLG video is tagged as what it is, which the+// window server shows brighter than SDR white where the layer allows it.static void describe_colours_of_video_surface(IOSurfaceRef surface, Media::CodingIndependentCodePoints cicp){auto matrix = CVYCbCrMatrixGetStringForIntegerCodePoint(to_underlying(cicp.matrix_coefficients()));auto primaries = CVColorPrimariesGetStringForIntegerCodePoint(to_underlying(cicp.color_primaries()));IOSurfaceSetValue(surface, CFSTR("IOSurfaceYCbCrMatrix"), matrix ? matrix : kCVImageBufferYCbCrMatrix_ITU_R_709_2);IOSurfaceSetValue(surface, CFSTR("IOSurfaceColorPrimaries"), primaries ? primaries : kCVImageBufferColorPrimaries_ITU_R_709_2);- IOSurfaceSetValue(surface, CFSTR("IOSurfaceTransferFunction"), kCVImageBufferTransferFunction_sRGB);++ CFStringRef transfer_function = kCVImageBufferTransferFunction_sRGB;CFStringRef color_space_name = kCGColorSpaceSRGB;- if (cicp.color_primaries() == Media::ColorPrimaries::SMPTE432)+ // The colour spaces named after ITU-R BT.2100 have BT.2020 primaries, so Display P3 video has names of its own.+ auto has_display_p3_primaries = cicp.color_primaries() == Media::ColorPrimaries::SMPTE432;+ if (cicp.transfer_characteristics() == Media::TransferCharacteristics::SMPTE2084) {+ transfer_function = kCVImageBufferTransferFunction_SMPTE_ST_2084_PQ;+ color_space_name = has_display_p3_primaries ? kCGColorSpaceDisplayP3_PQ : kCGColorSpaceITUR_2100_PQ;+ } else if (cicp.transfer_characteristics() == Media::TransferCharacteristics::HLG) {+ transfer_function = kCVImageBufferTransferFunction_ITU_R_2100_HLG;+ color_space_name = has_display_p3_primaries ? kCGColorSpaceDisplayP3_HLG : kCGColorSpaceITUR_2100_HLG;+ } else if (cicp.color_primaries() == Media::ColorPrimaries::SMPTE432) {color_space_name = kCGColorSpaceDisplayP3;- else if (cicp.color_primaries() == Media::ColorPrimaries::BT2020)+ } else if (cicp.color_primaries() == Media::ColorPrimaries::BT2020) {color_space_name = kCGColorSpaceITUR_2020_sRGBGamma;+ }+ IOSurfaceSetValue(surface, CFSTR("IOSurfaceTransferFunction"), transfer_function);IOSurfaceSetValue(surface, CFSTR("IOSurfaceColorSpace"), color_space_name);+ // A layer that prefers high dynamic range shows it only for contents tagged with their headroom.+ if (@available(macOS 15.0, *)) {+ if (holds_high_dynamic_range(cicp)) {+ auto headroom = assumed_headroom_of_high_dynamic_range_video;+ auto headroom_number = CFNumberCreate(kCFAllocatorDefault, kCFNumberFloatType, &headroom);+ IOSurfaceSetValue(surface, kIOSurfaceContentHeadroom, headroom_number);+ CFRelease(headroom_number);+ } else {+ IOSurfaceRemoveValue(surface, kIOSurfaceContentHeadroom);+ }+ }+}++// Extended dynamic range costs power, so a layer asks for it only while it shows content brighter than SDR white. The+// window server then tone maps the content to the headroom of the display it is on.+static void let_layer_show_high_dynamic_range(CALayer* layer, bool shows_high_dynamic_range)+{+ if (@available(macOS 26.0, *)) {+ layer.preferredDynamicRange = shows_high_dynamic_range ? CADynamicRangeHigh : CADynamicRangeStandard;+ } else {+ layer.wantsExtendedDynamicRangeContent = shows_high_dynamic_range;+ }}void RemoteLayer::show_content(ShownSublayer& shown_sublayer, RemoteSublayer& sublayer)@@ -374,6 +417,7 @@ void RemoteLayer::show_content(ShownSublayer& shown_sublayer, RemoteSublayer& suGfx::IntSize shown_size { min(visible_size.width(), surface_size.width()), min(visible_size.height(), surface_size.height()) };content_layer.contentsRect = unit_rect_of_surface_part(content_layer, shown_size, surface_size);content_layer.opaque = YES;+ let_layer_show_high_dynamic_range(content_layer, holds_high_dynamic_range(frame->cicp()));content_layer.contents = (__bridge id)iosurface.core_foundation_pointer();if (shown_frame)keep_video_frame_until_window_server_lets_go(*shown_frame);@@ -391,6 +435,7 @@ void RemoteLayer::show_content(ShownSublayer& shown_sublayer, RemoteSublayer& susurface.did_hand_to_window_server();content_layer.contentsRect = CGRectMake(0, 0, 1, 1);content_layer.opaque = NO;+ let_layer_show_high_dynamic_range(content_layer, false);content_layer.contents = (__bridge id)iosurface->core_foundation_pointer();shown_sublayer.content = move(lease);});