Skip to content

Truly independent web browser

Compositor: Let remote layers show sublayers above the frame

Aliaksandr Kalenikd0004d7d9ea5
diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cpp
index f7eaab108e9..532e128e207 100644
--- a/Services/Compositor/CompositorState.cpp
+++ b/Services/Compositor/CompositorState.cpp
@@ -1505,7 +1505,7 @@ void CompositorState::show_frame_in_remote_layer(Web::CompositorContextId contex
VERIFY(shared_image_buffer);
auto frame_size = pending_present.viewport_rect.size();
- remote_layer->value.layer->show_frame(*shared_image_buffer, frame_size, pending_present.device_pixel_ratio, remote_layer->value.fit_within);
+ remote_layer->value.layer->show_frame(shared_image_buffer, frame_size, pending_present.device_pixel_ratio, remote_layer->value.fit_within, {});
remote_layer->value.size_of_shown_frame_laid_out_by_page = frame_size == context.size_laid_out_by_page ? Optional<Gfx::IntSize> { frame_size } : OptionalNone {};
report_held_synchronized_resize_frames(remote_layer->key);
remote_layer->value.presentation->did_present_frame_in_remote_layer(context_id, pending_present.viewport_rect, pending_present.damage_rect, pending_present.device_pixel_ratio);
diff --git a/Services/Compositor/RemoteLayer.h b/Services/Compositor/RemoteLayer.h
index e71d7d75587..507f480e447 100644
--- a/Services/Compositor/RemoteLayer.h
+++ b/Services/Compositor/RemoteLayer.h
@@ -6,17 +6,51 @@
#pragma once
+#include <AK/HashMap.h>
#include <AK/Noncopyable.h>
+#include <AK/NonnullRefPtr.h>
#include <AK/Optional.h>
#include <AK/OwnPtr.h>
#include <AK/Platform.h>
+#include <AK/Time.h>
#include <AK/Types.h>
+#include <AK/Variant.h>
+#include <AK/Vector.h>
#include <AK/kmalloc.h>
+#include <Compositor/PresentableSurface.h>
+#include <LibCore/Forward.h>
+#include <LibGfx/AffineTransform.h>
#include <LibGfx/Forward.h>
+#include <LibGfx/Rect.h>
+#include <LibGfx/ScalingMode.h>
#include <LibGfx/Size.h>
+#include <LibMediaCodecs/VideoFrame.h>
namespace Compositor {
+// Content that the window server shows above the frame as it is, placed and clipped as drawing it into the frame would
+// have placed and clipped it. Geometry is in device pixels.
+struct RemoteSublayer {
+ struct RoundedClip {
+ Gfx::FloatRect rect;
+ float radius { 0 };
+ bool rounds_top_left { false };
+ bool rounds_top_right { false };
+ bool rounds_bottom_right { false };
+ bool rounds_bottom_left { false };
+ };
+
+ // Tells a sublayer apart from the others from one frame to the next, so that a frame changes only what moved.
+ u64 identity { 0 };
+ Variant<NonnullRefPtr<Media::VideoFrame>, PresentableSurface::Lease> content;
+ // Where the content is drawn, in its own space, which the transform maps into the frame.
+ Gfx::IntRect content_rect;
+ Gfx::AffineTransform content_to_frame;
+ Gfx::FloatRect clip_rect;
+ Optional<RoundedClip> rounded_clip;
+ Gfx::ScalingMode scaling_mode { Gfx::ScalingMode::Bilinear };
+};
+
// A CoreAnimation layer tree that this process commits frames into, and that another process shows in a view of its
// own by the ID of the tree's context. Frames then reach the screen without passing through that process.
class RemoteLayer {
@@ -33,8 +67,9 @@ public:
u32 core_animation_context_id() const { return m_core_animation_context_id; }
// Shows the part of the buffer that the frame was painted into, at the top left of the layer at its own size, or
- // scaled to fit within the size of the view given, in device pixels.
- void show_frame(Gfx::SharedImageBuffer const&, Gfx::IntSize painted_size, double device_pixel_ratio, Optional<Gfx::IntSize> fit_within);
+ // scaled to fit within the size of the view given, in device pixels, with the sublayers above it, bottom to top.
+ // Without a buffer, the frame shown before stays, and only the sublayers change.
+ void show_frame(Gfx::SharedImageBuffer const*, Gfx::IntSize painted_size, double device_pixel_ratio, Optional<Gfx::IntSize> fit_within, Vector<RemoteSublayer>);
// Places the frame on display again, as when the view that it is scaled into changes size.
void place_frame(Optional<Gfx::IntSize> fit_within);
@@ -47,13 +82,37 @@ private:
RemoteLayer() = default;
#ifdef AK_OS_MACOS
+ struct ShownSublayer {
+ void* clip_layer { nullptr };
+ void* rounded_clip_layer { nullptr };
+ void* content_layer { nullptr };
+ Variant<Empty, NonnullRefPtr<Media::VideoFrame>, PresentableSurface::Lease> content;
+ };
+
void lay_out_frame(Optional<Gfx::IntSize> fit_within);
+ void show_sublayers(Vector<RemoteSublayer>&);
+ void update_shown_sublayer(ShownSublayer&, RemoteSublayer&);
+ void show_content(ShownSublayer&, RemoteSublayer&);
+ void stop_showing_sublayer(ShownSublayer&);
+ void keep_video_frame_until_window_server_lets_go(NonnullRefPtr<Media::VideoFrame>);
+ void release_video_frames_window_server_let_go_of();
void* m_context { nullptr };
void* m_root_layer { nullptr };
+ void* m_frame_layer { nullptr };
void* m_content_layer { nullptr };
Gfx::IntSize m_painted_size;
double m_device_pixel_ratio { 1.0 };
+ HashMap<u64, ShownSublayer> m_shown_sublayers;
+ // A decoder decodes into the surface of a frame again once nothing holds the frame, so a replaced frame is held
+ // until the window server can no longer be reading it. Its surface reads as in use for as long as the decoder's
+ // pixel buffer lives, which is as long as the frame does, so that cannot tell.
+ struct ReplacedVideoFrame {
+ NonnullRefPtr<Media::VideoFrame> frame;
+ MonotonicTime replaced_at;
+ };
+ Vector<ReplacedVideoFrame> m_replaced_video_frames;
+ RefPtr<Core::Timer> m_video_frame_release_timer;
#endif
u32 m_core_animation_context_id { 0 };
};
@@ -66,7 +125,7 @@ inline OwnPtr<RemoteLayer> RemoteLayer::try_create()
inline RemoteLayer::~RemoteLayer() = default;
-inline void RemoteLayer::show_frame(Gfx::SharedImageBuffer const&, Gfx::IntSize, double, Optional<Gfx::IntSize>)
+inline void RemoteLayer::show_frame(Gfx::SharedImageBuffer const*, Gfx::IntSize, double, Optional<Gfx::IntSize>, Vector<RemoteSublayer>)
{
}
diff --git a/Services/Compositor/RemoteLayer.mm b/Services/Compositor/RemoteLayer.mm
index 5dcffa48ddf..65a8113c394 100644
--- a/Services/Compositor/RemoteLayer.mm
+++ b/Services/Compositor/RemoteLayer.mm
@@ -4,14 +4,19 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/HashTable.h>
#include <AK/StdLibExtras.h>
#include <Compositor/RemoteLayer.h>
#include <LibCompositing/FrameFitting.h>
+#include <LibCore/Timer.h>
#include <LibGfx/SharedImageBuffer.h>
+#include <LibMediaCodecs/VideoSurface.h>
#include <dlfcn.h>
#include <objc/runtime.h>
+#import <CoreVideo/CoreVideo.h>
+#import <IOSurface/IOSurfaceRef.h>
#import <QuartzCore/QuartzCore.h>
// A CAContext carries a layer tree to the window server, where a CALayerHost in another process shows it by the
@@ -64,17 +69,23 @@ static void commit_layer_changes(Callback callback)
}
}
+static bool s_changes_are_held_off_screen = false;
+static MonotonicTime s_changes_last_released_on_screen;
+
// The transactions of each change nest in this one until it is committed, so none of them reaches the window server
// before then.
void RemoteLayer::hold_changes_on_screen()
{
[CATransaction begin];
+ s_changes_are_held_off_screen = true;
}
void RemoteLayer::release_changes_held_off_screen()
{
[CATransaction commit];
[CATransaction flush];
+ s_changes_are_held_off_screen = false;
+ s_changes_last_released_on_screen = MonotonicTime::now();
}
static CALayer* as_layer(void* layer)
@@ -96,15 +107,21 @@ OwnPtr<RemoteLayer> RemoteLayer::try_create()
// Frames are laid out from the top left, as the view that hosts the tree is.
root_layer.geometryFlipped = YES;
+ // The frame and the sublayers over it are laid out in the frame's own pixels, and scaled together to fit.
+ auto* frame_layer = [[CALayer alloc] init];
+ frame_layer.anchorPoint = CGPointZero;
+ [root_layer addSublayer:frame_layer];
+
auto* content_layer = [[CALayer alloc] init];
content_layer.anchorPoint = CGPointZero;
- [root_layer addSublayer:content_layer];
+ [frame_layer addSublayer:content_layer];
id<LadybirdCoreAnimationContext> context = [(Class<LadybirdCoreAnimationContextClass>)context_class contextWithCGSConnection:connection_id_function() options:@ {}];
context.layer = root_layer;
remote_layer->m_context = [context retain];
remote_layer->m_root_layer = root_layer;
+ remote_layer->m_frame_layer = frame_layer;
remote_layer->m_content_layer = content_layer;
remote_layer->m_core_animation_context_id = context.contextId;
});
@@ -116,26 +133,44 @@ OwnPtr<RemoteLayer> RemoteLayer::try_create()
RemoteLayer::~RemoteLayer()
{
commit_layer_changes([&] {
+ for (auto& it : m_shown_sublayers)
+ stop_showing_sublayer(it.value);
auto context = static_cast<id<LadybirdCoreAnimationContext>>(m_context);
context.layer = nil;
[context release];
[as_layer(m_content_layer) release];
+ [as_layer(m_frame_layer) release];
[as_layer(m_root_layer) release];
});
+ if (m_video_frame_release_timer)
+ m_video_frame_release_timer->stop();
}
-static CGRect in_points(Gfx::IntRect rect, double device_pixel_ratio)
+static CGRect in_points(Gfx::FloatRect rect, double device_pixel_ratio)
{
return CGRectMake(rect.x() / device_pixel_ratio, rect.y() / device_pixel_ratio, rect.width() / device_pixel_ratio, rect.height() / device_pixel_ratio);
}
+static CGRect in_points(Gfx::IntRect rect, double device_pixel_ratio)
+{
+ return in_points(rect.to_type<float>(), device_pixel_ratio);
+}
+
// The root layer spans the view a frame is fitted into, as its y-axis is flipped within its own height.
void RemoteLayer::lay_out_frame(Optional<Gfx::IntSize> fit_within)
{
+ Gfx::IntRect frame_rect { {}, m_painted_size };
Gfx::IntRect root_rect { {}, fit_within.value_or(m_painted_size) };
- auto content_rect = fit_within.has_value() ? Compositing::rect_fitting_frame(m_painted_size, *fit_within) : root_rect;
+ auto fitted_rect = fit_within.has_value() ? Compositing::rect_fitting_frame(m_painted_size, *fit_within) : root_rect;
as_layer(m_root_layer).frame = in_points(root_rect, m_device_pixel_ratio);
- as_layer(m_content_layer).frame = in_points(content_rect, m_device_pixel_ratio);
+
+ auto* frame_layer = as_layer(m_frame_layer);
+ frame_layer.bounds = in_points(frame_rect, m_device_pixel_ratio);
+ frame_layer.position = in_points(fitted_rect, m_device_pixel_ratio).origin;
+ frame_layer.affineTransform = CGAffineTransformMakeScale(
+ static_cast<CGFloat>(fitted_rect.width()) / max(frame_rect.width(), 1),
+ static_cast<CGFloat>(fitted_rect.height()) / max(frame_rect.height(), 1));
+ as_layer(m_content_layer).frame = in_points(frame_rect, m_device_pixel_ratio);
}
void RemoteLayer::place_frame(Optional<Gfx::IntSize> fit_within)
@@ -147,31 +182,260 @@ void RemoteLayer::place_frame(Optional<Gfx::IntSize> fit_within)
});
}
-void RemoteLayer::show_frame(Gfx::SharedImageBuffer const& buffer, Gfx::IntSize painted_size, double device_pixel_ratio, Optional<Gfx::IntSize> fit_within)
+// The part of a surface that a layer shows, as a unit rectangle, which turns upside down along with the layer's y-axis.
+static CGRect unit_rect_of_surface_part(CALayer* layer, Gfx::IntSize part_size, Gfx::IntSize surface_size)
+{
+ auto unit_width = static_cast<CGFloat>(part_size.width()) / surface_size.width();
+ auto unit_height = static_cast<CGFloat>(part_size.height()) / surface_size.height();
+ return layer.contentsAreFlipped
+ ? CGRectMake(0, 0, unit_width, unit_height)
+ : CGRectMake(0, 1 - unit_height, unit_width, unit_height);
+}
+
+void RemoteLayer::show_frame(Gfx::SharedImageBuffer const* buffer, Gfx::IntSize painted_size, double device_pixel_ratio, Optional<Gfx::IntSize> fit_within, Vector<RemoteSublayer> sublayers)
{
- auto const& iosurface = buffer.iosurface_handle();
- auto surface_width = static_cast<int>(iosurface.width());
- auto surface_height = static_cast<int>(iosurface.height());
- auto painted_width = min(painted_size.width(), surface_width);
- auto painted_height = min(painted_size.height(), surface_height);
- if (painted_width <= 0 || painted_height <= 0)
+ Gfx::IntSize surface_size;
+ if (buffer) {
+ auto const& iosurface = buffer->iosurface_handle();
+ surface_size = { static_cast<int>(iosurface.width()), static_cast<int>(iosurface.height()) };
+ // A store can be larger than the frame painted into it, as while the window is resized.
+ painted_size = { min(painted_size.width(), surface_size.width()), min(painted_size.height(), surface_size.height()) };
+ if (painted_size.is_empty())
+ return;
+ m_painted_size = painted_size;
+ }
+ if (m_painted_size.is_empty())
return;
- m_painted_size = { painted_width, painted_height };
m_device_pixel_ratio = device_pixel_ratio;
commit_layer_changes([&] {
auto* content_layer = as_layer(m_content_layer);
- auto painted_unit_width = static_cast<CGFloat>(painted_width) / surface_width;
- auto painted_unit_height = static_cast<CGFloat>(painted_height) / surface_height;
- // A store can be larger than the frame painted into it, as while the window is resized. The unit rectangle
- // that picks the painted part turns upside down along with the layer's y-axis.
- content_layer.contentsRect = content_layer.contentsAreFlipped
- ? CGRectMake(0, 0, painted_unit_width, painted_unit_height)
- : CGRectMake(0, 1 - painted_unit_height, painted_unit_width, painted_unit_height);
- content_layer.contentsScale = device_pixel_ratio;
+ if (buffer) {
+ content_layer.contentsRect = unit_rect_of_surface_part(content_layer, m_painted_size, surface_size);
+ content_layer.contentsScale = device_pixel_ratio;
+ }
lay_out_frame(fit_within);
- content_layer.contents = (__bridge id)iosurface.core_foundation_pointer();
+ if (buffer)
+ content_layer.contents = (__bridge id)buffer->iosurface_handle().core_foundation_pointer();
+ show_sublayers(sublayers);
+ });
+ release_video_frames_window_server_let_go_of();
+}
+
+void RemoteLayer::show_sublayers(Vector<RemoteSublayer>& sublayers)
+{
+ HashTable<u64> shown_identities;
+ auto* ordered_layers = [NSMutableArray arrayWithObject:as_layer(m_content_layer)];
+ for (auto& sublayer : sublayers) {
+ auto& shown_sublayer = m_shown_sublayers.ensure(sublayer.identity, [] {
+ ShownSublayer shown_sublayer;
+ auto* clip_layer = [[CALayer alloc] init];
+ clip_layer.anchorPoint = CGPointZero;
+ clip_layer.masksToBounds = YES;
+ auto* content_layer = [[CALayer alloc] init];
+ content_layer.anchorPoint = CGPointZero;
+ [clip_layer addSublayer:content_layer];
+ shown_sublayer.clip_layer = clip_layer;
+ shown_sublayer.content_layer = content_layer;
+ return shown_sublayer;
+ });
+ update_shown_sublayer(shown_sublayer, sublayer);
+ [ordered_layers addObject:as_layer(shown_sublayer.clip_layer)];
+ shown_identities.set(sublayer.identity);
+ }
+
+ Vector<u64> identities_no_longer_shown;
+ for (auto& it : m_shown_sublayers) {
+ if (!shown_identities.contains(it.key))
+ identities_no_longer_shown.append(it.key);
+ }
+ for (auto identity : identities_no_longer_shown) {
+ auto shown_sublayer = m_shown_sublayers.take(identity).release_value();
+ stop_showing_sublayer(shown_sublayer);
+ }
+
+ as_layer(m_frame_layer).sublayers = ordered_layers;
+}
+
+static CGAffineTransform to_core_graphics(Gfx::AffineTransform const& transform)
+{
+ return CGAffineTransformMake(transform.a(), transform.b(), transform.c(), transform.d(), transform.e(), transform.f());
+}
+
+static NSString* filter_for_scaling_mode(Gfx::ScalingMode scaling_mode)
+{
+ switch (scaling_mode) {
+ case Gfx::ScalingMode::None:
+ case Gfx::ScalingMode::NearestNeighbor:
+ return kCAFilterNearest;
+ case Gfx::ScalingMode::Bilinear:
+ case Gfx::ScalingMode::BilinearMipmap:
+ return kCAFilterLinear;
+ }
+ VERIFY_NOT_REACHED();
+}
+
+void RemoteLayer::update_shown_sublayer(ShownSublayer& shown_sublayer, RemoteSublayer& sublayer)
+{
+ auto device_pixel_ratio = m_device_pixel_ratio;
+ auto* clip_layer = as_layer(shown_sublayer.clip_layer);
+ auto* content_layer = as_layer(shown_sublayer.content_layer);
+ clip_layer.frame = in_points(sublayer.clip_rect, device_pixel_ratio);
+
+ // The content layer is placed relative to the innermost layer that clips it.
+ auto origin_of_content_parent = sublayer.clip_rect.location();
+ if (sublayer.rounded_clip.has_value()) {
+ auto const& rounded_clip = *sublayer.rounded_clip;
+ if (!shown_sublayer.rounded_clip_layer) {
+ auto* rounded_clip_layer = [[CALayer alloc] init];
+ rounded_clip_layer.anchorPoint = CGPointZero;
+ rounded_clip_layer.masksToBounds = YES;
+ [clip_layer addSublayer:rounded_clip_layer];
+ [rounded_clip_layer addSublayer:content_layer];
+ shown_sublayer.rounded_clip_layer = rounded_clip_layer;
+ }
+ auto* rounded_clip_layer = as_layer(shown_sublayer.rounded_clip_layer);
+ rounded_clip_layer.frame = in_points(rounded_clip.rect.translated(-origin_of_content_parent), device_pixel_ratio);
+ rounded_clip_layer.cornerRadius = rounded_clip.radius / device_pixel_ratio;
+ // The corners are named in the layer's own coordinates, whose y-axis points down here like the frame's.
+ CACornerMask masked_corners = 0;
+ if (rounded_clip.rounds_top_left)
+ masked_corners |= kCALayerMinXMinYCorner;
+ if (rounded_clip.rounds_top_right)
+ masked_corners |= kCALayerMaxXMinYCorner;
+ if (rounded_clip.rounds_bottom_right)
+ masked_corners |= kCALayerMaxXMaxYCorner;
+ if (rounded_clip.rounds_bottom_left)
+ masked_corners |= kCALayerMinXMaxYCorner;
+ rounded_clip_layer.maskedCorners = masked_corners;
+ origin_of_content_parent = rounded_clip.rect.location();
+ } else if (shown_sublayer.rounded_clip_layer) {
+ [clip_layer addSublayer:content_layer];
+ [as_layer(shown_sublayer.rounded_clip_layer) removeFromSuperlayer];
+ [as_layer(shown_sublayer.rounded_clip_layer) release];
+ shown_sublayer.rounded_clip_layer = nullptr;
+ }
+
+ // Maps the layer's points to the content's pixels, through the frame's pixels, to the points of its parent.
+ auto content_rect = sublayer.content_rect;
+ auto content_width_in_points = content_rect.width() / device_pixel_ratio;
+ auto content_height_in_points = content_rect.height() / device_pixel_ratio;
+ auto transform = CGAffineTransformMakeScale(device_pixel_ratio, device_pixel_ratio);
+ transform = CGAffineTransformConcat(transform, CGAffineTransformMakeTranslation(content_rect.x(), content_rect.y()));
+ transform = CGAffineTransformConcat(transform, to_core_graphics(sublayer.content_to_frame));
+ transform = CGAffineTransformConcat(transform, CGAffineTransformMakeScale(1 / device_pixel_ratio, 1 / device_pixel_ratio));
+ transform = CGAffineTransformConcat(transform, CGAffineTransformMakeTranslation(-origin_of_content_parent.x() / device_pixel_ratio, -origin_of_content_parent.y() / device_pixel_ratio));
+ auto const* lease = sublayer.content.get_pointer<PresentableSurface::Lease>();
+ if (lease && lease->surface().content_is_upside_down())
+ transform = CGAffineTransformConcat(CGAffineTransformMake(1, 0, 0, -1, 0, content_height_in_points), transform);
+ content_layer.bounds = CGRectMake(0, 0, content_width_in_points, content_height_in_points);
+ content_layer.position = CGPointZero;
+ content_layer.affineTransform = transform;
+ content_layer.allowsEdgeAntialiasing = !sublayer.content_to_frame.is_identity_or_translation_or_scale();
+ content_layer.contentsScale = device_pixel_ratio;
+ content_layer.magnificationFilter = filter_for_scaling_mode(sublayer.scaling_mode);
+ content_layer.minificationFilter = sublayer.scaling_mode == Gfx::ScalingMode::BilinearMipmap ? kCAFilterTrilinear : filter_for_scaling_mode(sublayer.scaling_mode);
+
+ show_content(shown_sublayer, sublayer);
+}
+
+// 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.
+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 color_space_name = kCGColorSpaceSRGB;
+ if (cicp.color_primaries() == Media::ColorPrimaries::SMPTE432)
+ color_space_name = kCGColorSpaceDisplayP3;
+ else if (cicp.color_primaries() == Media::ColorPrimaries::BT2020)
+ color_space_name = kCGColorSpaceITUR_2020_sRGBGamma;
+ IOSurfaceSetValue(surface, CFSTR("IOSurfaceColorSpace"), color_space_name);
+}
+
+void RemoteLayer::show_content(ShownSublayer& shown_sublayer, RemoteSublayer& sublayer)
+{
+ auto* content_layer = as_layer(shown_sublayer.content_layer);
+ sublayer.content.visit(
+ [&](NonnullRefPtr<Media::VideoFrame>& frame) {
+ auto const* shown_frame = shown_sublayer.content.get_pointer<NonnullRefPtr<Media::VideoFrame>>();
+ if (shown_frame && shown_frame->ptr() == frame.ptr())
+ return;
+ auto video_surface = frame->surface();
+ VERIFY(video_surface);
+ auto const& iosurface = video_surface->io_surface();
+ describe_colours_of_video_surface(static_cast<IOSurfaceRef>(iosurface.core_foundation_pointer()), frame->cicp());
+ Gfx::IntSize surface_size { static_cast<int>(iosurface.width()), static_cast<int>(iosurface.height()) };
+ auto 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()) };
+ content_layer.contentsRect = unit_rect_of_surface_part(content_layer, shown_size, surface_size);
+ content_layer.opaque = YES;
+ content_layer.contents = (__bridge id)iosurface.core_foundation_pointer();
+ if (shown_frame)
+ keep_video_frame_until_window_server_lets_go(*shown_frame);
+ shown_sublayer.content = frame;
+ },
+ [&](PresentableSurface::Lease& lease) {
+ if (auto const* shown_frame = shown_sublayer.content.get_pointer<NonnullRefPtr<Media::VideoFrame>>())
+ keep_video_frame_until_window_server_lets_go(*shown_frame);
+ auto const* shown_lease = shown_sublayer.content.get_pointer<PresentableSurface::Lease>();
+ if (shown_lease && &shown_lease->surface() == &lease.surface())
+ return;
+ auto& surface = lease.surface();
+ auto const* iosurface = surface.iosurface();
+ VERIFY(iosurface);
+ surface.did_hand_to_window_server();
+ content_layer.contentsRect = CGRectMake(0, 0, 1, 1);
+ content_layer.opaque = NO;
+ content_layer.contents = (__bridge id)iosurface->core_foundation_pointer();
+ shown_sublayer.content = move(lease);
+ });
+}
+
+void RemoteLayer::stop_showing_sublayer(ShownSublayer& shown_sublayer)
+{
+ if (auto const* shown_frame = shown_sublayer.content.get_pointer<NonnullRefPtr<Media::VideoFrame>>())
+ keep_video_frame_until_window_server_lets_go(*shown_frame);
+ shown_sublayer.content = Empty {};
+ [as_layer(shown_sublayer.clip_layer) removeFromSuperlayer];
+ [as_layer(shown_sublayer.content_layer) release];
+ if (shown_sublayer.rounded_clip_layer)
+ [as_layer(shown_sublayer.rounded_clip_layer) release];
+ [as_layer(shown_sublayer.clip_layer) release];
+}
+
+void RemoteLayer::keep_video_frame_until_window_server_lets_go(NonnullRefPtr<Media::VideoFrame> frame)
+{
+ m_replaced_video_frames.append({ move(frame), MonotonicTime::now() });
+ if (!m_video_frame_release_timer) {
+ m_video_frame_release_timer = Core::Timer::create_repeating(AK::Duration::from_milliseconds(25), [this] {
+ release_video_frames_window_server_let_go_of();
+ });
+ }
+ if (!m_video_frame_release_timer->is_active())
+ m_video_frame_release_timer->start();
+}
+
+// The window server shows a replacement from its next display refresh on, and a composite that started before may
+// still be reading the frame replaced. A few refreshes later, even at 60 Hz, it has let go of it.
+static constexpr AK::Duration time_window_server_may_read_a_replaced_surface = AK::Duration::from_milliseconds(50);
+
+void RemoteLayer::release_video_frames_window_server_let_go_of()
+{
+ // A replacement made while changes are held off screen reaches the window server only once they are released.
+ if (s_changes_are_held_off_screen)
+ return;
+ auto now = MonotonicTime::now();
+ m_replaced_video_frames.remove_all_matching([&](auto const& replaced) {
+ auto shown_on_screen_since = max(replaced.replaced_at, s_changes_last_released_on_screen);
+ return now - shown_on_screen_since >= time_window_server_may_read_a_replaced_surface;
});
+ if (m_replaced_video_frames.is_empty() && m_video_frame_release_timer)
+ m_video_frame_release_timer->stop();
}
}