Truly independent web browser
LibWeb+Compositor: Draw willReadFrequently 2D canvases in memory
Aliaksandr Kalenik42f3fd0bb7d8
diff --git a/Libraries/LibCompositing/Rust/src/display_list/canvas_2d_recorder.rs b/Libraries/LibCompositing/Rust/src/display_list/canvas_2d_recorder.rsindex 70e5ec5975e..d301dd289cd 100644--- a/Libraries/LibCompositing/Rust/src/display_list/canvas_2d_recorder.rs+++ b/Libraries/LibCompositing/Rust/src/display_list/canvas_2d_recorder.rs@@ -34,7 +34,7 @@ pub trait Transport {type VideoFrameHold: ?Sized;type Bitmap;- fn create_context(&self, size: IntSize, alpha: bool) -> bool;+ fn create_context(&self, size: IntSize, alpha: bool, will_read_frequently: bool) -> bool;fn canvas_id(&self) -> Option<CanvasId>;fn destroy_context(&self);@@ -66,8 +66,8 @@ impl<T: Transport> Recorder<T> {}}- pub fn create_context(&self, size: IntSize, alpha: bool) -> bool {- self.transport.create_context(size, alpha)+ pub fn create_context(&self, size: IntSize, alpha: bool, will_read_frequently: bool) -> bool {+ self.transport.create_context(size, alpha, will_read_frequently)}pub fn canvas_id(&self) -> Option<CanvasId> {@@ -259,7 +259,7 @@ mod tests {type VideoFrameHold = u32;type Bitmap = IntRect;- fn create_context(&self, size: IntSize, _alpha: bool) -> bool {+ fn create_context(&self, size: IntSize, _alpha: bool, _will_read_frequently: bool) -> bool {size.width > 0 && size.height > 0}@@ -398,7 +398,7 @@ mod tests {#[test]fn the_calls_of_the_context_reach_the_transport() {let recorder = Recorder::new(FakeTransport::default());- assert!(recorder.create_context(IntSize { width: 2, height: 2 }, true));+ assert!(recorder.create_context(IntSize { width: 2, height: 2 }, true, false));assert_eq!(recorder.canvas_id(), Some(CanvasId(5)));recorder.present();recorder.flush();diff --git a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/context.rs b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/context.rsindex 5da33fa7260..15c6b1fcee3 100644--- a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/context.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/context.rs@@ -344,9 +344,12 @@ impl ContextState {// FIXME: implement context attribute .color_space// FIXME: implement context attribute .color_type// FIXME: implement context attribute .desynchronized- // FIXME: implement context attribute .will_read_frequentlylet recorder = Recorder::new(transport);- if !recorder.create_context(self.size.get(), self.context_attributes.alpha) {+ if !recorder.create_context(+ self.size.get(),+ self.context_attributes.alpha,+ self.context_attributes.will_read_frequently,+ ) {return false;}self.backing_storage.replace(Some(BackingStorage { recorder }));diff --git a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/host.rs b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/host.rsindex df8f05c5230..02dbb6eaa05 100644--- a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/host.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/host.rs@@ -17,7 +17,13 @@ use libgfx_rust::{IntRect, IntSize};unsafe extern "C" {fn web_canvas_2d_transport_unref(transport: *mut c_void);- fn web_canvas_2d_transport_create_context(transport: *mut c_void, width: i32, height: i32, alpha: bool) -> bool;+ fn web_canvas_2d_transport_create_context(+ transport: *mut c_void,+ width: i32,+ height: i32,+ alpha: bool,+ will_read_frequently: bool,+ ) -> bool;fn web_canvas_2d_transport_canvas_id(transport: *const c_void, out_canvas_id: *mut u64) -> bool;fn web_canvas_2d_transport_destroy_context(transport: *mut c_void);fn web_canvas_2d_transport_append_command(transport: *mut c_void, record: ak::Span<'_, u8>);@@ -157,8 +163,10 @@ impl Transport for CppTransport {type VideoFrameHold = LentVideoFrameHold;type Bitmap = Bitmap;- fn create_context(&self, size: IntSize, alpha: bool) -> bool {- unsafe { web_canvas_2d_transport_create_context(self.as_ptr(), size.width, size.height, alpha) }+ fn create_context(&self, size: IntSize, alpha: bool, will_read_frequently: bool) -> bool {+ unsafe {+ web_canvas_2d_transport_create_context(self.as_ptr(), size.width, size.height, alpha, will_read_frequently)+ }}fn canvas_id(&self) -> Option<CanvasId> {diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_pattern.rs b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_pattern.rsindex e1ef0574caf..0e19b4aac83 100644--- a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_pattern.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_pattern.rs@@ -50,7 +50,9 @@ fn read_back_current_video_frame(video: CppInterface) -> Option<ImageFrameHandlewidth,height,};- if width <= 0 || height <= 0 || !recorder.create_context(IntSize { width, height }, true) {+ // NB: The context only exists to be read back.+ let will_read_frequently = true;+ if width <= 0 || height <= 0 || !recorder.create_context(IntSize { width, height }, true, will_read_frequently) {return None;}diff --git a/Libraries/LibWeb/Compositor/CompositorConnection.cpp b/Libraries/LibWeb/Compositor/CompositorConnection.cppindex 02ebd3452c8..366ec908b63 100644--- a/Libraries/LibWeb/Compositor/CompositorConnection.cpp+++ b/Libraries/LibWeb/Compositor/CompositorConnection.cpp@@ -309,7 +309,7 @@ void CompositorConnection::set_video_sink_ticking(Media::VideoSinkHandle video_sasync_set_video_sink_ticking(video_sink_handle, should_tick);}-Optional<Compositing::CanvasId> CompositorConnection::create_canvas_2d_context(Gfx::IntSize size, bool alpha)+Optional<Compositing::CanvasId> CompositorConnection::create_canvas_2d_context(Gfx::IntSize size, bool alpha, bool will_read_frequently){if (!can_send_message_to_compositor())return {};@@ -334,7 +334,7 @@ Optional<Compositing::CanvasId> CompositorConnection::create_canvas_2d_context(Gauto canvas_id = Compositing::CanvasId { m_next_reserved_canvas_id++ };--m_reserved_canvas_ids_remaining;- async_create_canvas_2d_context(canvas_id, size, alpha);+ async_create_canvas_2d_context(canvas_id, size, alpha, will_read_frequently);return canvas_id;}diff --git a/Libraries/LibWeb/Compositor/CompositorConnection.h b/Libraries/LibWeb/Compositor/CompositorConnection.hindex 9f591ed77f7..7ea9355c56c 100644--- a/Libraries/LibWeb/Compositor/CompositorConnection.h+++ b/Libraries/LibWeb/Compositor/CompositorConnection.h@@ -55,7 +55,7 @@ public:void remove_video_sink(Media::VideoSinkHandle);void release_video_frame_hold(Media::VideoSinkHandle, u64 generation);void set_video_sink_ticking(Media::VideoSinkHandle, bool should_tick);- Optional<Compositing::CanvasId> create_canvas_2d_context(Gfx::IntSize, bool alpha);+ Optional<Compositing::CanvasId> create_canvas_2d_context(Gfx::IntSize, bool alpha, bool will_read_frequently);void update_canvas_2d_stream(Compositing::Canvas2DCommandStream&);void destroy_canvas_context(Compositing::CanvasId);Gfx::ShareableBitmap get_canvas_pixels(Compositing::CanvasId, Gfx::IntRect, Gfx::AlphaType = Gfx::AlphaType::Premultiplied);diff --git a/Libraries/LibWeb/Compositor/CompositorHost.cpp b/Libraries/LibWeb/Compositor/CompositorHost.cppindex b8a2d6b21d4..48f122001b0 100644--- a/Libraries/LibWeb/Compositor/CompositorHost.cpp+++ b/Libraries/LibWeb/Compositor/CompositorHost.cpp@@ -270,10 +270,10 @@ public:}private:- virtual bool create_context(Gfx::IntSize size, bool alpha) override+ virtual bool create_context(Gfx::IntSize size, bool alpha, bool will_read_frequently) override{VERIFY(!m_canvas_id.has_value());- auto canvas_id = m_connection->create_canvas_2d_context(size, alpha);+ auto canvas_id = m_connection->create_canvas_2d_context(size, alpha, will_read_frequently);if (!canvas_id.has_value())return false;m_canvas_id = *canvas_id;diff --git a/Libraries/LibWeb/HTML/Canvas/RemoteCanvas2DTransport.h b/Libraries/LibWeb/HTML/Canvas/RemoteCanvas2DTransport.hindex ce8d54f860c..5d58e68b4e1 100644--- a/Libraries/LibWeb/HTML/Canvas/RemoteCanvas2DTransport.h+++ b/Libraries/LibWeb/HTML/Canvas/RemoteCanvas2DTransport.h@@ -26,7 +26,7 @@ class WEB_API RemoteCanvas2DTransport : public RefCounted<RemoteCanvas2DTransporpublic:virtual ~RemoteCanvas2DTransport() = default;- virtual bool create_context(Gfx::IntSize, bool alpha) = 0;+ virtual bool create_context(Gfx::IntSize, bool alpha, bool will_read_frequently) = 0;virtual Optional<Compositing::CanvasId> canvas_id() const = 0;virtual void destroy_context() = 0;diff --git a/Libraries/LibWeb/HTML/Canvas/RemoteCanvas2DTransportFFI.cpp b/Libraries/LibWeb/HTML/Canvas/RemoteCanvas2DTransportFFI.cppindex 231328778f6..ac3fbb9cbc8 100644--- a/Libraries/LibWeb/HTML/Canvas/RemoteCanvas2DTransportFFI.cpp+++ b/Libraries/LibWeb/HTML/Canvas/RemoteCanvas2DTransportFFI.cpp@@ -34,7 +34,7 @@ static_assert(to_underlying(Gfx::AlphaType::Unpremultiplied) == 1);extern "C" {void web_canvas_2d_transport_unref(void* transport);-bool web_canvas_2d_transport_create_context(void* transport, i32 width, i32 height, bool alpha);+bool web_canvas_2d_transport_create_context(void* transport, i32 width, i32 height, bool alpha, bool will_read_frequently);bool web_canvas_2d_transport_canvas_id(void const* transport, u64* out_canvas_id);void web_canvas_2d_transport_destroy_context(void* transport);void web_canvas_2d_transport_append_command(void* transport, ReadonlyBytes record);@@ -62,9 +62,9 @@ void web_canvas_2d_transport_unref(void* transport)transport_from(transport).unref();}-bool web_canvas_2d_transport_create_context(void* transport, i32 width, i32 height, bool alpha)+bool web_canvas_2d_transport_create_context(void* transport, i32 width, i32 height, bool alpha, bool will_read_frequently){- return transport_from(transport).create_context({ width, height }, alpha);+ return transport_from(transport).create_context({ width, height }, alpha, will_read_frequently);}bool web_canvas_2d_transport_canvas_id(void const* transport, u64* out_canvas_id)diff --git a/Services/Compositor/CanvasHost.cpp b/Services/Compositor/CanvasHost.cppindex e58285aae7b..ada4a3c12d2 100644--- a/Services/Compositor/CanvasHost.cpp+++ b/Services/Compositor/CanvasHost.cpp@@ -29,7 +29,10 @@ struct CanvasHost::VideoFrameImage {u64 generation { 0 };// Kept until replaced, since the GPU samples the image after the commands are played.RefPtr<Media::VideoFrame> frame;- sk_sp<SkImage> image;+ sk_sp<SkImage> image_on_gpu;+ // NB: A canvas that draws in memory does not read back image_on_gpu instead: Skia caches readbacks by image ID,+ // and the image of a video surface keeps its ID while the surface shows new frames.+ sk_sp<SkImage> image_in_memory;};CanvasHost::CanvasHost(RefPtr<Gfx::SkiaBackendContext> skia_backend_context, CanvasSurfaceRegistry& canvas_surface_registry, VideoFrameHolds const& video_frame_holds)@@ -46,11 +49,17 @@ CanvasHost::~CanvasHost()m_canvas_surface_registry.remove_canvas_surface(canvas_id);}-OwnPtr<Canvas2DCommandPlayer> CanvasHost::create_2d_command_player(Gfx::IntSize size, bool alpha)+OwnPtr<Canvas2DCommandPlayer> CanvasHost::create_2d_command_player(Gfx::IntSize size, bool alpha, bool will_read_frequently){if (!Compositing::is_valid_canvas_2d_size(size))return nullptr;+ // https://html.spec.whatwg.org/multipage/canvas.html#concept-canvas-will-read-frequently+ // "When a CanvasSettings object's will read frequently is true, the user agent may optimize the canvas for+ // readback operations."+ // NB: The canvas then draws in memory, where a readback copies its pixels instead of waiting for the GPU.+ auto skia_backend_context_of_canvas = will_read_frequently ? nullptr : m_skia_backend_context;+auto format = alpha ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;auto canvas_surface_resolver = [this](u64 canvas_id) -> Gfx::PaintingSurface const* {// A 2D source resolves to its live draw surface: the shared command@@ -75,10 +84,10 @@ OwnPtr<Canvas2DCommandPlayer> CanvasHost::create_2d_command_player(Gfx::IntSizereturn nullptr;return &it->value;};- auto video_frame_image_resolver = [this](u64 video_sink_handle, u64 generation) {- return held_video_frame_image(Media::VideoSinkHandle { video_sink_handle }, generation);+ auto video_frame_image_resolver = [this, skia_backend_context_of_canvas](u64 video_sink_handle, u64 generation) {+ return held_video_frame_image(Media::VideoSinkHandle { video_sink_handle }, generation, skia_backend_context_of_canvas);};- auto player = make<Canvas2DCommandPlayer>(m_skia_backend_context, size, format, Gfx::AlphaType::Premultiplied,+ auto player = make<Canvas2DCommandPlayer>(skia_backend_context_of_canvas, size, format, Gfx::AlphaType::Premultiplied,Canvas2DCommandPlayer::Resolvers {.canvas_surface = move(canvas_surface_resolver),.font = move(font_resolver),@@ -100,13 +109,13 @@ static void copy_surface_contents(Gfx::PaintingSurface& source, Gfx::PaintingSurdestination.copy_from_surface(source);}-static NonnullRefPtr<Gfx::PaintingSurface> create_presented_canvas_surface(Gfx::PaintingSurface& source)+static NonnullRefPtr<Gfx::PaintingSurface> create_presented_canvas_surface(Gfx::PaintingSurface& source, RefPtr<Gfx::SkiaBackendContext> skia_backend_context){auto surface = Gfx::PaintingSurface::create_with_size(source.size(),Gfx::BitmapFormat::BGRA8888,Gfx::AlphaType::Premultiplied,- source.skia_backend_context());+ move(skia_backend_context));copy_surface_contents(source, *surface);return surface;}@@ -118,13 +127,15 @@ HostWebGLContext& CanvasHost::as_webgl(Context& context)return **webgl_context;}-bool CanvasHost::create_2d_context(Compositing::CanvasId canvas_id, Gfx::IntSize size, bool alpha)+bool CanvasHost::create_2d_context(Compositing::CanvasId canvas_id, Gfx::IntSize size, bool alpha, bool will_read_frequently){- auto command_player = create_2d_command_player(size, alpha);+ auto command_player = create_2d_command_player(size, alpha, will_read_frequently);if (!command_player)return false;- auto presented_surface = create_presented_canvas_surface(command_player->surface());+ // NB: A canvas that draws in memory still presents onto the GPU, if there is one. A present then uploads it once,+ // instead of every composited frame uploading a fresh snapshot of it.+ auto presented_surface = create_presented_canvas_surface(command_player->surface(), m_skia_backend_context);m_canvas_surface_registry.set_canvas_surface(m_canvas_surface_owner_id, canvas_id, presented_surface);Canvas2DContext context {.command_player = command_player.release_nonnull(),@@ -211,20 +222,30 @@ void CanvasHost::execute_canvas_2d_stream(Vector<Compositing::Canvas2DCommandStr}}-sk_sp<SkImage> CanvasHost::held_video_frame_image(Media::VideoSinkHandle handle, u64 generation)+sk_sp<SkImage> CanvasHost::held_video_frame_image(Media::VideoSinkHandle handle, u64 generation, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context_of_canvas){- if (auto cached = m_video_frame_images.get(handle); cached.has_value() && (*cached)->generation == generation)- return (*cached)->image;+ auto* video_frame_image = m_video_frame_images.get(handle).value_or(nullptr);+ if (!video_frame_image || video_frame_image->generation != generation) {+ auto frame = m_video_frame_holds.held_frame(handle, generation);+ if (!frame)+ return nullptr;+ auto new_video_frame_image = make<VideoFrameImage>(generation, move(frame), sk_sp<SkImage> {}, sk_sp<SkImage> {});+ video_frame_image = new_video_frame_image.ptr();+ m_video_frame_images.set(handle, move(new_video_frame_image));+ }- m_drew_video_frame_in_segment = true;- auto frame = m_video_frame_holds.held_frame(handle, generation);- if (!frame)- return nullptr;- auto image = sk_image_from_video_frame(*frame, m_skia_backend_context);- if (!image)- return nullptr;- m_video_frame_images.set(handle, make<VideoFrameImage>(generation, move(frame), image));- return image;+ if (!skia_backend_context_of_canvas) {+ if (!video_frame_image->image_in_memory)+ video_frame_image->image_in_memory = sk_image_in_memory_from_video_frame(*video_frame_image->frame, m_skia_backend_context);+ return video_frame_image->image_in_memory;+ }++ if (!video_frame_image->image_on_gpu) {+ video_frame_image->image_on_gpu = sk_image_from_video_frame(*video_frame_image->frame, skia_backend_context_of_canvas);+ if (video_frame_image->image_on_gpu)+ m_drew_video_frame_in_segment = true;+ }+ return video_frame_image->image_on_gpu;}void CanvasHost::forget_video_sink(Media::VideoSinkHandle handle)diff --git a/Services/Compositor/CanvasHost.h b/Services/Compositor/CanvasHost.hindex af023a3c6ee..1357b2ad26e 100644--- a/Services/Compositor/CanvasHost.h+++ b/Services/Compositor/CanvasHost.h@@ -60,7 +60,7 @@ public:CanvasHost(RefPtr<Gfx::SkiaBackendContext>, CanvasSurfaceRegistry&, VideoFrameHolds const&);~CanvasHost();- bool create_2d_context(Compositing::CanvasId, Gfx::IntSize, bool alpha);+ bool create_2d_context(Compositing::CanvasId, Gfx::IntSize, bool alpha, bool will_read_frequently);CreateWebGLContextResult create_webgl_context(Compositing::WebGL::WebGLVersion, Gfx::IntSize, bool depth, bool stencil, bool antialias);void destroy_context(Compositing::CanvasId);@@ -93,10 +93,10 @@ private:using Context = Variant<Canvas2DContext, WebGLContext>;Context* context(Compositing::CanvasId);- OwnPtr<Canvas2DCommandPlayer> create_2d_command_player(Gfx::IntSize, bool alpha);+ OwnPtr<Canvas2DCommandPlayer> create_2d_command_player(Gfx::IntSize, bool alpha, bool will_read_frequently);static HostWebGLContext& as_webgl(Context&);void present_canvas_2d_context(Compositing::CanvasId, Canvas2DContext&);- sk_sp<SkImage> held_video_frame_image(Media::VideoSinkHandle, u64 generation);+ sk_sp<SkImage> held_video_frame_image(Media::VideoSinkHandle, u64 generation, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context_of_canvas);Vector<HostWebGLContext::UploadSource> resolve_webgl_upload_sources(Vector<Compositing::WebGL::ImageSourceFrame> const&) const;RefPtr<Gfx::SkiaBackendContext> m_skia_backend_context;diff --git a/Services/Compositor/CompositorWebContentServer.ipc b/Services/Compositor/CompositorWebContentServer.ipcindex f7c00b452d0..f1e46967e72 100644--- a/Services/Compositor/CompositorWebContentServer.ipc+++ b/Services/Compositor/CompositorWebContentServer.ipc@@ -39,7 +39,7 @@ endpoint CompositorWebContentServer// Hands WebContent a range of canvas IDs it may name its own 2D contexts with, so creating one needs no round trip.reserve_canvas_ids() => (Compositing::CanvasId first_canvas_id, u64 count)- create_canvas_2d_context(Compositing::CanvasId canvas_id, Gfx::IntSize size, bool alpha) =|+ create_canvas_2d_context(Compositing::CanvasId canvas_id, Gfx::IntSize size, bool alpha, bool will_read_frequently) =|// The segments draw with the fonts of the previous batch named in kept_font_ids, and with added_fonts. The other// fonts of the previous batch are released. The image frames are those that the segments draw.update_canvas_2d_stream(Vector<Compositing::Canvas2DCommandStreamSegment> segments, Vector<Compositing::FontResourceId> kept_font_ids, Vector<Compositing::DisplayListFontResource> added_fonts, Vector<Compositing::DisplayListImageFrameResource> image_frames) =|diff --git a/Services/Compositor/ConnectionFromWebContent.cpp b/Services/Compositor/ConnectionFromWebContent.cppindex eee03995f84..8dac08b208f 100644--- a/Services/Compositor/ConnectionFromWebContent.cpp+++ b/Services/Compositor/ConnectionFromWebContent.cpp@@ -279,7 +279,7 @@ Messages::CompositorWebContentServer::ReserveCanvasIdsResponse ConnectionFromWebreturn { first_canvas_id, reserved_canvas_id_count };}-void ConnectionFromWebContent::create_canvas_2d_context(Compositing::CanvasId canvas_id, Gfx::IntSize size, bool alpha)+void ConnectionFromWebContent::create_canvas_2d_context(Compositing::CanvasId canvas_id, Gfx::IntSize size, bool alpha, bool will_read_frequently){// NB: WebContent names its contexts with the IDs of its range in order, so each ID names one context only.if (m_reserved_canvas_ids.is_used_up() || canvas_id.value() != m_reserved_canvas_ids.next) {@@ -288,7 +288,7 @@ void ConnectionFromWebContent::create_canvas_2d_context(Compositing::CanvasId ca}++m_reserved_canvas_ids.next;// NB: A context that cannot be created is not a protocol violation: commands recorded for it are dropped.- (void)m_canvas_host.create_2d_context(canvas_id, size, alpha);+ (void)m_canvas_host.create_2d_context(canvas_id, size, alpha, will_read_frequently);}void ConnectionFromWebContent::update_canvas_2d_stream(Vector<Compositing::Canvas2DCommandStreamSegment> segments, Vector<Compositing::FontResourceId> kept_font_ids, Vector<Compositing::DisplayListFontResource> added_fonts, Vector<Compositing::DisplayListImageFrameResource> image_frames)diff --git a/Services/Compositor/ConnectionFromWebContent.h b/Services/Compositor/ConnectionFromWebContent.hindex 23d15281eb6..cae64f01032 100644--- a/Services/Compositor/ConnectionFromWebContent.h+++ b/Services/Compositor/ConnectionFromWebContent.h@@ -55,7 +55,7 @@ private:virtual void update_scroll_state(Web::CompositorContextId, Compositing::ScrollStateSnapshot, Compositing::KeyboardScrollState) override;virtual void update_display_list_resources(Web::CompositorContextId, Compositing::DisplayListResourceTransaction) override;virtual Messages::CompositorWebContentServer::ReserveCanvasIdsResponse reserve_canvas_ids() override;- virtual void create_canvas_2d_context(Compositing::CanvasId, Gfx::IntSize, bool) override;+ virtual void create_canvas_2d_context(Compositing::CanvasId, Gfx::IntSize, bool alpha, bool will_read_frequently) override;virtual void update_canvas_2d_stream(Vector<Compositing::Canvas2DCommandStreamSegment>, Vector<Compositing::FontResourceId>, Vector<Compositing::DisplayListFontResource>, Vector<Compositing::DisplayListImageFrameResource>) override;virtual void destroy_canvas_context(Compositing::CanvasId) override;virtual Messages::CompositorWebContentServer::GetCanvasPixelsResponse get_canvas_pixels(Compositing::CanvasId, Gfx::IntRect, Gfx::AlphaType) override;diff --git a/Services/Compositor/VideoFrameSkia.cpp b/Services/Compositor/VideoFrameSkia.cppindex b17a427f056..033f9fe5e78 100644--- a/Services/Compositor/VideoFrameSkia.cpp+++ b/Services/Compositor/VideoFrameSkia.cpp@@ -218,6 +218,14 @@ static sk_sp<SkImage> image_for_video_surface(Media::VideoFrame const& frame, Mereturn cached_image->image;}+static Gfx::ColorSpace color_space_of(Media::VideoFrame const& frame)+{+ auto color_space_result = Gfx::ColorSpace::from_cicp(frame.cicp());+ if (color_space_result.is_error())+ return {};+ return color_space_result.release_value();+}+sk_sp<SkImage> sk_image_from_video_frame(Media::VideoFrame const& frame, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context){sk_sp<SkImage> image;@@ -231,9 +239,7 @@ sk_sp<SkImage> sk_image_from_video_frame(Media::VideoFrame const& frame, RefPtr<if (!image && !yuv_data.has_value() && surface == nullptr)return nullptr;- auto color_space = Gfx::ColorSpace {};- if (auto color_space_result = Gfx::ColorSpace::from_cicp(frame.cicp()); !color_space_result.is_error())- color_space = color_space_result.release_value();+ auto color_space = color_space_of(frame);if (!image && gr_context && yuv_data.has_value()) {image = SkImages::TextureFromYUVAPixmaps(@@ -261,27 +267,49 @@ sk_sp<SkImage> sk_image_from_video_frame(Media::VideoFrame const& frame, RefPtr<return image;}+// Draws the frame's surface on the GPU into a fresh surface and reads its pixels back from there, in sRGB.+static ErrorOr<NonnullRefPtr<Gfx::Bitmap>> read_back_video_surface_through_gpu(Media::VideoFrame const& frame, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context)+{+ auto image = sk_image_from_video_frame(frame, skia_backend_context);+ if (!image)+ return Error::from_string_literal("Could not make an image of the video frame's surface");+ auto size = frame.size().to_type<int>();+ auto surface = Gfx::PaintingSurface::create_with_size(size, Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, skia_backend_context);+ auto rect = SkRect::MakeIWH(size.width(), size.height());+ surface->canvas().drawImageRect(image.get(), rect, rect, Gfx::to_skia_sampling_options(Gfx::ScalingMode::NearestNeighbor), nullptr, SkCanvas::kStrict_SrcRectConstraint);+ auto bitmap = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, size));+ surface->read_into_bitmap(*bitmap);+ return bitmap;+}++sk_sp<SkImage> sk_image_in_memory_from_video_frame(Media::VideoFrame const& frame, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context)+{+ auto bitmap_or_error = frame.to_bitmap();+ if (!bitmap_or_error.is_error())+ return Gfx::sk_image_adopting_bitmap(bitmap_or_error.release_value(), color_space_of(frame));++ // NB: Only macOS reads a video surface on the CPU. Elsewhere the GPU reads it, into a surface of its own: a readback+ // of the image of the video surface could be one Skia cached for an earlier frame that the surface showed.+ if (frame.surface() != nullptr && skia_backend_context) {+ bitmap_or_error = read_back_video_surface_through_gpu(frame, skia_backend_context);+ if (!bitmap_or_error.is_error())+ return Gfx::sk_image_adopting_bitmap(bitmap_or_error.release_value(), {});+ }++ dbgln("Could not convert video frame to bitmap: {}", bitmap_or_error.release_error());+ return nullptr;+}+ErrorOr<Gfx::DecodedImageFrame> decoded_image_frame_from_video_frame(Media::VideoFrame const& frame, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context){if (frame.surface() != nullptr && skia_backend_context) {- auto image = sk_image_from_video_frame(frame, skia_backend_context);- if (!image)- return Error::from_string_literal("Could not make an image of the video frame's surface");- auto size = frame.size().to_type<int>();- auto surface = Gfx::PaintingSurface::create_with_size(size, Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, skia_backend_context);- auto rect = SkRect::MakeIWH(size.width(), size.height());- surface->canvas().drawImageRect(image.get(), rect, rect, Gfx::to_skia_sampling_options(Gfx::ScalingMode::NearestNeighbor), nullptr, SkCanvas::kStrict_SrcRectConstraint);- auto bitmap = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, size));+ auto bitmap = TRY(read_back_video_surface_through_gpu(frame, skia_backend_context));// The pixels are read back in sRGB.- surface->read_into_bitmap(*bitmap);return Gfx::DecodedImageFrame { NonnullRefPtr<Gfx::Bitmap const> { move(bitmap) } };}auto bitmap = TRY(frame.to_bitmap());- auto color_space = Gfx::ColorSpace {};- if (auto color_space_result = Gfx::ColorSpace::from_cicp(frame.cicp()); !color_space_result.is_error())- color_space = color_space_result.release_value();- return Gfx::DecodedImageFrame { NonnullRefPtr<Gfx::Bitmap const> { move(bitmap) }, move(color_space) };+ return Gfx::DecodedImageFrame { NonnullRefPtr<Gfx::Bitmap const> { move(bitmap) }, color_space_of(frame) };}}diff --git a/Services/Compositor/VideoFrameSkia.h b/Services/Compositor/VideoFrameSkia.hindex c96a9848beb..c8760ea6410 100644--- a/Services/Compositor/VideoFrameSkia.h+++ b/Services/Compositor/VideoFrameSkia.h@@ -40,6 +40,10 @@ void video_surface_image_will_show_new_frame(SkImage&);// Makes an image of the frame's pixels, on the GPU when there is a backend context, and in memory otherwise.sk_sp<SkImage> sk_image_from_video_frame(Media::VideoFrame const&, RefPtr<Gfx::SkiaBackendContext> const&);+// Makes an image of the frame's pixels in memory, for a canvas that draws there. The CPU converts the frame where it can+// read it, and the GPU, when there is one, reads back a surface that only it can read.+sk_sp<SkImage> sk_image_in_memory_from_video_frame(Media::VideoFrame const&, RefPtr<Gfx::SkiaBackendContext> const&);+// Reads the frame's pixels into memory. A surface is drawn on the GPU and read back from there, since only the GPU// can read some surfaces.ErrorOr<Gfx::DecodedImageFrame> decoded_image_frame_from_video_frame(Media::VideoFrame const&, RefPtr<Gfx::SkiaBackendContext> const&);