Truly independent web browser
LibGfx+LibWeb+Compositor: Hold image frame pixels in shared memory
Aliaksandr Kalenikd310f83d5e78
diff --git a/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.cpp b/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.cppindex 0ff143f703e..85c32947415 100644--- a/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.cpp+++ b/Libraries/LibCompositing/DisplayList/DisplayListResourceStorage.cpp@@ -91,8 +91,8 @@ static constexpr size_t max_sampled_pixels = 1000;static bool classify_image_frame_for_force_dark(Gfx::DecodedImageFrame const& frame){- auto const& bitmap = frame.bitmap();- auto size = bitmap.size();+ auto bitmap = frame.bitmap_sharing_pixels();+ auto size = bitmap->size();if (size.is_empty())return false;@@ -104,7 +104,7 @@ static bool classify_image_frame_for_force_dark(Gfx::DecodedImageFrame const& frsize_t sampled_count = 0;for (int y = 0; y < size.height(); y += step) {for (int x = 0; x < size.width(); x += step) {- auto color = bitmap.get_pixel(x, y);+ auto color = bitmap->get_pixel(x, y);sampled_count++;// A pixel this sheer says something about the image's shape rather than its palette — so it's counted// toward transparency, but kept out of the palette samples.diff --git a/Libraries/LibCompositing/DisplayList/DisplayListResourceTransaction.cpp b/Libraries/LibCompositing/DisplayList/DisplayListResourceTransaction.cppindex e2faa37a415..05b145f1502 100644--- a/Libraries/LibCompositing/DisplayList/DisplayListResourceTransaction.cpp+++ b/Libraries/LibCompositing/DisplayList/DisplayListResourceTransaction.cpp@@ -7,32 +7,11 @@#include <AK/Math.h>#include <LibCompositing/DisplayList/DisplayList.h>#include <LibCompositing/DisplayList/DisplayListResourceStorage.h>-#include <LibGfx/Bitmap.h>#include <LibGfx/Font/Font.h>#include <LibGfx/Font/Typeface.h>-#include <LibGfx/ShareableBitmap.h>#include <LibIPC/Decoder.h>#include <LibIPC/Encoder.h>-namespace Compositing {--static ErrorOr<Gfx::ShareableBitmap> shareable_bitmap_from_image_frame(Gfx::DecodedImageFrame const& frame)-{- auto bitmap = frame.bitmap().to_shareable_bitmap();- if (!bitmap.is_valid())- return Error::from_string_literal("Display-list resource transaction failed to create image-frame bitmap");- return bitmap;-}--static ErrorOr<Gfx::DecodedImageFrame> create_image_frame_from_ipc(Gfx::ShareableBitmap bitmap, Gfx::ColorSpace color_space)-{- if (!bitmap.is_valid() || !bitmap.bitmap())- return Error::from_string_literal("Display-list resource transaction contained invalid image-frame bitmap");- return Gfx::DecodedImageFrame { *bitmap.bitmap(), move(color_space) };-}--}-namespace IPC {template<>@@ -76,8 +55,7 @@ template<>ErrorOr<void> encode(Encoder& encoder, Compositing::DisplayListImageFrameResource const& resource){TRY(encoder.encode(resource.id));- TRY(encoder.encode(TRY(Compositing::shareable_bitmap_from_image_frame(resource.frame))));- TRY(encoder.encode(resource.frame.color_space()));+ TRY(encoder.encode(resource.frame));return {};}@@ -85,12 +63,11 @@ template<>ErrorOr<Compositing::DisplayListImageFrameResource> decode(Decoder& decoder){auto id = TRY(decoder.decode<Compositing::ImageFrameResourceId>());- auto bitmap = TRY(decoder.decode<Gfx::ShareableBitmap>());- auto color_space = TRY(decoder.decode<Gfx::ColorSpace>());+ auto frame = TRY(decoder.decode<Gfx::DecodedImageFrame>());return Compositing::DisplayListImageFrameResource {.id = id,- .frame = TRY(Compositing::create_image_frame_from_ipc(move(bitmap), move(color_space))),+ .frame = move(frame),};}diff --git a/Libraries/LibCore/AnonymousBuffer.h b/Libraries/LibCore/AnonymousBuffer.hindex b729b4f5ec7..39cda94791e 100644--- a/Libraries/LibCore/AnonymousBuffer.h+++ b/Libraries/LibCore/AnonymousBuffer.h@@ -43,6 +43,7 @@ public:AnonymousBuffer() = default;bool is_valid() const { return m_impl; }+ bool has_other_holders() const { return m_impl && m_impl->ref_count() > 1; }ErrorOr<AnonymousBuffer> snapshot() const;ErrorOr<AnonymousBuffer> snapshot(size_t offset, size_t size) const;diff --git a/Libraries/LibGfx/DecodedImageFrame.cpp b/Libraries/LibGfx/DecodedImageFrame.cppindex b3dcb19c5ba..c3417f0753c 100644--- a/Libraries/LibGfx/DecodedImageFrame.cpp+++ b/Libraries/LibGfx/DecodedImageFrame.cpp@@ -4,10 +4,11 @@* SPDX-License-Identifier: BSD-2-Clause*/+#include <AK/Checked.h>#include <LibGfx/DecodedImageFrame.h>-#include <LibGfx/ShareableBitmap.h>#include <LibIPC/Decoder.h>#include <LibIPC/Encoder.h>+#include <LibIPC/File.h>#include <RustFFI.h>extern "C" {@@ -33,13 +34,64 @@ extern "C" void ladybird_gfx_decoded_image_frame_release(void* frame)delete static_cast<Gfx::DecodedImageFrame*>(frame);}+namespace Gfx {++ErrorOr<DecodedImageFrame> DecodedImageFrame::create_over_shared_memory(Core::AnonymousBuffer shared_memory, size_t offset_of_pixels, IntSize size, BitmapFormat format, AlphaType alpha_type, ColorSpace color_space)+{+ if (!is_valid_bitmap_format(to_underlying(format)))+ return Error::from_string_literal("Gfx::DecodedImageFrame format is invalid");+ if (size.is_empty() || Bitmap::size_would_overflow(format, size))+ return Error::from_string_literal("Gfx::DecodedImageFrame size is invalid");++ auto size_of_pixels = Bitmap::size_in_bytes(Bitmap::minimum_pitch(size.width(), format), size.height());+ if (offset_of_pixels % alignof(RawPixel) != 0 || offset_of_pixels > shared_memory.size() || size_of_pixels > shared_memory.size() - offset_of_pixels)+ return Error::from_string_literal("Gfx::DecodedImageFrame pixels are outside of the shared memory");++ return DecodedImageFrame { move(shared_memory), offset_of_pixels, size, format, alpha_type, move(color_space) };+}++ErrorOr<DecodedImageFrame> DecodedImageFrame::create(Bitmap const& bitmap, ColorSpace color_space)+{+ if (bitmap.anonymous_buffer().is_valid())+ return create_over_shared_memory(bitmap.anonymous_buffer(), 0, bitmap.size(), bitmap.format(), bitmap.alpha_type(), move(color_space));++ if (bitmap.size().is_empty())+ return Error::from_string_literal("Gfx::DecodedImageFrame size is invalid");++ auto pitch = Bitmap::minimum_pitch(bitmap.width(), bitmap.format());+ auto shared_memory = TRY(Core::AnonymousBuffer::create_with_size(Bitmap::size_in_bytes(pitch, bitmap.height())));+ auto* pixels = shared_memory.data<u8>();+ for (int y = 0; y < bitmap.height(); ++y)+ __builtin_memcpy(pixels + static_cast<size_t>(y) * pitch, bitmap.scanline_u8(y), pitch);+ return create_over_shared_memory(move(shared_memory), 0, bitmap.size(), bitmap.format(), bitmap.alpha_type(), move(color_space));+}++ErrorOr<DecodedImageFrame> DecodedImageFrame::create_with_id(u64 id, Bitmap const& bitmap, ColorSpace color_space)+{+ auto frame = TRY(create(bitmap, move(color_space)));+ frame.m_id = id;+ return frame;+}++NonnullRefPtr<Bitmap const> DecodedImageFrame::bitmap_sharing_pixels() const+{+ if (m_offset_of_pixels == 0)+ return MUST(Bitmap::create_with_anonymous_buffer(m_format, m_alpha_type, m_shared_memory, m_size));+ return MUST(Bitmap::create_wrapper(m_format, m_alpha_type, m_size, pitch(), const_cast<u8*>(pixels().data()), [shared_memory = m_shared_memory] { }));+}++}+namespace IPC {template<>ErrorOr<void> encode(Encoder& encoder, Gfx::DecodedImageFrame const& frame){- auto bitmap = frame.bitmap().to_shareable_bitmap();- TRY(encoder.encode(bitmap));+ TRY(encoder.encode(TRY(IPC::File::clone_fd(frame.shared_memory().fd()))));+ TRY(encoder.encode(static_cast<u64>(frame.offset_of_pixels())));+ TRY(encoder.encode(frame.size()));+ TRY(encoder.encode(static_cast<u32>(frame.format())));+ TRY(encoder.encode(static_cast<u32>(frame.alpha_type())));TRY(encoder.encode(frame.color_space()));return {};}@@ -47,9 +99,35 @@ ErrorOr<void> encode(Encoder& encoder, Gfx::DecodedImageFrame const& frame)template<>ErrorOr<Gfx::DecodedImageFrame> decode(Decoder& decoder){- auto bitmap = TRY(decoder.decode<Gfx::ShareableBitmap>());+ auto shared_memory_file = TRY(decoder.decode<IPC::File>());+ auto offset_of_pixels = TRY(decoder.decode<u64>());++ auto size = TRY(decoder.decode<Gfx::IntSize>());+ if (size.is_empty())+ return Error::from_string_literal("IPC: Invalid Gfx::DecodedImageFrame size");++ auto raw_format = TRY(decoder.decode<u32>());+ if (!Gfx::is_valid_bitmap_format(raw_format))+ return Error::from_string_literal("IPC: Invalid Gfx::DecodedImageFrame format");+ auto format = static_cast<Gfx::BitmapFormat>(raw_format);+ if (Gfx::Bitmap::size_would_overflow(format, size))+ return Error::from_string_literal("IPC: Gfx::DecodedImageFrame size overflow");++ auto raw_alpha_type = TRY(decoder.decode<u32>());+ if (!Gfx::is_valid_alpha_type(raw_alpha_type))+ return Error::from_string_literal("IPC: Invalid Gfx::DecodedImageFrame alpha type");+ auto alpha_type = static_cast<Gfx::AlphaType>(raw_alpha_type);+auto color_space = TRY(decoder.decode<Gfx::ColorSpace>());- return Gfx::DecodedImageFrame { *bitmap.bitmap(), move(color_space) };++ Checked<u64> end_of_pixels = offset_of_pixels;+ end_of_pixels += Gfx::Bitmap::size_in_bytes(Gfx::Bitmap::minimum_pitch(size.width(), format), size.height());+ if (end_of_pixels.has_overflow())+ return Error::from_string_literal("IPC: Gfx::DecodedImageFrame pixels overflow");++ auto shared_memory = TRY(Core::AnonymousBuffer::create_from_anon_fd(shared_memory_file.take_fd(), static_cast<size_t>(end_of_pixels.value())));+ TRY(shared_memory.validate_backing_size());+ return Gfx::DecodedImageFrame::create_over_shared_memory(move(shared_memory), static_cast<size_t>(offset_of_pixels), size, format, alpha_type, move(color_space));}}diff --git a/Libraries/LibGfx/DecodedImageFrame.h b/Libraries/LibGfx/DecodedImageFrame.hindex 6e48b04799d..059c69afb6c 100644--- a/Libraries/LibGfx/DecodedImageFrame.h+++ b/Libraries/LibGfx/DecodedImageFrame.h@@ -6,10 +6,12 @@#pragma once-#include <AK/Assertions.h>#include <AK/Atomic.h>+#include <AK/Error.h>#include <AK/NonnullRefPtr.h>+#include <AK/Span.h>#include <AK/kmalloc.h>+#include <LibCore/AnonymousBuffer.h>#include <LibGC/Forward.h>#include <LibGfx/Bitmap.h>#include <LibGfx/ColorSpace.h>@@ -23,49 +25,52 @@ class DecodedImageFrame final {public:AK_ALLOC_WITH_KMALLOC;- DecodedImageFrame(NonnullRefPtr<Bitmap const> bitmap, ColorSpace color_space = {})- : m_id(next_id())- , m_bitmap(move(bitmap))- , m_color_space(move(color_space))- {- }-- DecodedImageFrame(Bitmap const& bitmap, ColorSpace color_space = {})- : DecodedImageFrame(NonnullRefPtr<Bitmap const> { bitmap }, move(color_space))- {- }+ static ErrorOr<DecodedImageFrame> create(Bitmap const&, ColorSpace = {});+ static ErrorOr<DecodedImageFrame> create_over_shared_memory(Core::AnonymousBuffer shared_memory, size_t offset_of_pixels, IntSize, BitmapFormat, AlphaType, ColorSpace = {});// For the same pixels decoded again, which caches keyed by the id of a frame may still hold.- static DecodedImageFrame with_id(u64 id, NonnullRefPtr<Bitmap const> bitmap, ColorSpace color_space = {})- {- DecodedImageFrame frame { move(bitmap), move(color_space) };- frame.m_id = id;- return frame;- }+ static ErrorOr<DecodedImageFrame> create_with_id(u64 id, Bitmap const&, ColorSpace = {});- DecodedImageFrame(Bitmap const& bitmap, AlphaType alpha_type, ColorSpace color_space = {})- : DecodedImageFrame(bitmap_with_alpha_type(bitmap, alpha_type), move(color_space))+ DecodedImageFrame with_new_id() const{+ auto frame = *this;+ frame.m_id = next_id();+ return frame;}- Bitmap const& bitmap() const { return *m_bitmap; }- NonnullRefPtr<Bitmap const> bitmap_ref() const { return m_bitmap; }ColorSpace const& color_space() const { return m_color_space; }u64 id() const { return m_id; }- int width() const { return m_bitmap->width(); }- int height() const { return m_bitmap->height(); }- IntRect rect() const { return m_bitmap->rect(); }- IntSize size() const { return m_bitmap->size(); }+ BitmapFormat format() const { return m_format; }+ AlphaType alpha_type() const { return m_alpha_type; }++ int width() const { return m_size.width(); }+ int height() const { return m_size.height(); }+ IntRect rect() const { return { {}, m_size }; }+ IntSize size() const { return m_size; }++ size_t pitch() const { return Bitmap::minimum_pitch(m_size.width(), m_format); }+ size_t size_in_bytes() const { return Bitmap::size_in_bytes(pitch(), m_size.height()); }+ ReadonlyBytes pixels() const { return { m_shared_memory.data<u8>() + m_offset_of_pixels, size_in_bytes() }; }++ Core::AnonymousBuffer const& shared_memory() const { return m_shared_memory; }+ size_t offset_of_pixels() const { return m_offset_of_pixels; }++ bool has_same_pixels_as(DecodedImageFrame const& other) const { return pixels().data() == other.pixels().data(); }+ bool pixels_are_held_elsewhere() const { return m_shared_memory.has_other_holders(); }++ NonnullRefPtr<Bitmap const> bitmap_sharing_pixels() const;private:- static NonnullRefPtr<Bitmap const> bitmap_with_alpha_type(Bitmap const& bitmap, AlphaType alpha_type)+ DecodedImageFrame(Core::AnonymousBuffer shared_memory, size_t offset_of_pixels, IntSize size, BitmapFormat format, AlphaType alpha_type, ColorSpace color_space)+ : m_id(next_id())+ , m_shared_memory(move(shared_memory))+ , m_offset_of_pixels(offset_of_pixels)+ , m_size(size)+ , m_format(format)+ , m_alpha_type(alpha_type)+ , m_color_space(move(color_space)){- if (bitmap.alpha_type() == alpha_type)- return NonnullRefPtr<Bitmap const> { bitmap };- auto new_bitmap = MUST(bitmap.clone());- new_bitmap->set_alpha_type_destructive(alpha_type);- return new_bitmap;}static u64 next_id()@@ -75,7 +80,11 @@ private:}u64 m_id { 0 };- NonnullRefPtr<Bitmap const> m_bitmap;+ Core::AnonymousBuffer m_shared_memory;+ size_t m_offset_of_pixels { 0 };+ IntSize m_size;+ BitmapFormat m_format { BitmapFormat::Invalid };+ AlphaType m_alpha_type { AlphaType::Premultiplied };ColorSpace m_color_space;};diff --git a/Libraries/LibGfx/Rust/src/image_frame.rs b/Libraries/LibGfx/Rust/src/image_frame.rsindex 0bcd999af76..e7181b911fd 100644--- a/Libraries/LibGfx/Rust/src/image_frame.rs+++ b/Libraries/LibGfx/Rust/src/image_frame.rs@@ -29,9 +29,9 @@ struct ImageFrameEntry {raw: NonNull<c_void>,}-// SAFETY: The entry owns an immutable Gfx::DecodedImageFrame copy. Its bitmap is-// atomically reference counted, and neither the frame nor its color space is-// mutated while the entry is live.+// SAFETY: The entry owns an immutable Gfx::DecodedImageFrame copy. The shared+// memory of its pixels is atomically reference counted, and neither the frame+// nor its color space is mutated while the entry is live.unsafe impl Send for ImageFrameEntry {}// SAFETY: See the Send implementation above.unsafe impl Sync for ImageFrameEntry {}diff --git a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/host.rs b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/host.rsindex 94786b00af8..8fbaeab8c9c 100644--- a/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/host.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/canvas_2d/host.rs@@ -85,11 +85,14 @@ impl Bitmap {bitmap}- /// A frame of the bitmap, whose pixels are in the color space, which the frame keeps alive.- pub(crate) fn into_image_frame(self, color_space: ColorSpace) -> ImageFrameHandle {+ /// A frame of the pixels of the bitmap, which are in the color space, or none if one cannot be made.+ pub(crate) fn into_image_frame(self, color_space: ColorSpace) -> Option<ImageFrameHandle> {let frame = unsafe { web_canvas_2d_image_frame_from_bitmap(self.into_raw(), color_space) };- // SAFETY: LibWeb hands over a frame that it allocated with new, which took over the reference to the bitmap.- unsafe { ImageFrameHandle::adopt(frame) }+ if frame.is_null() {+ return None;+ }+ // SAFETY: LibWeb hands over a frame that it allocated with new.+ Some(unsafe { ImageFrameHandle::adopt(frame) })}pub(crate) fn size_in_bytes(&self) -> usize {diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_pattern.rs b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_pattern.rsindex a79fea929a7..45a3d866a64 100644--- a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_pattern.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_pattern.rs@@ -80,7 +80,7 @@ pub(crate) fn read_back_current_video_frame(video: CppInterface, color_space: Corecorder.flush();let bitmap = recorder.read_back_pixels(frame_rect, AlphaType::Premultiplied, color_space);recorder.destroy_context();- Some(bitmap?.into_image_frame(color_space))+ bitmap?.into_image_frame(color_space)}#[derive(Trace)]diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rs b/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rsindex eaaf88da0a4..8687d3d24ed 100644--- a/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rs@@ -39,7 +39,7 @@ struct FramePixels {}unsafe extern "C" {- // Defined in Libraries/LibWeb/HTML/ImageBitmap.cpp, which encodes and decodes the Gfx::Bitmap of a transferred+ // Defined in Libraries/LibWeb/HTML/ImageBitmap.cpp, which encodes and decodes the pixels of a transferred// frame. A frame that it hands over is a Gfx::DecodedImageFrame allocated with new, or null for none.fn web_image_bitmap_frame_size_in_bytes(frame: *const c_void) -> usize;fn web_image_bitmap_frame_pixels(frame: *const c_void, pixels: &mut FramePixels);diff --git a/Libraries/LibWeb/Compositor/CompositorConnection.cpp b/Libraries/LibWeb/Compositor/CompositorConnection.cppindex 60bd961a18d..58ddd21a937 100644--- a/Libraries/LibWeb/Compositor/CompositorConnection.cpp+++ b/Libraries/LibWeb/Compositor/CompositorConnection.cpp@@ -424,19 +424,7 @@ void CompositorConnection::update_canvas_2d_stream(Compositing::Canvas2DCommandSadded_fonts.append(move(font));}- // A frame travels in a shared bitmap. One that cannot be placed in one is left out, and the Compositor skips the- // draws that name it.- Vector<Compositing::DisplayListImageFrameResource> image_frames;- for (auto& frame : stream.take_image_frames()) {- auto bitmap = frame.frame.bitmap().to_shareable_bitmap();- if (!bitmap.is_valid()) {- dbgln("WebContent: Could not place a {} canvas image frame in shared memory", frame.frame.bitmap().size());- continue;- }- image_frames.append({ .id = frame.id, .frame = Gfx::DecodedImageFrame { *bitmap.bitmap(), frame.frame.color_space() } });- }-- auto encoded_message = MUST(Messages::CompositorWebContentServer::UpdateCanvas2dStream::static_encode(stream.take_segments(), move(kept_font_ids), move(added_fonts), move(image_frames)));+ auto encoded_message = MUST(Messages::CompositorWebContentServer::UpdateCanvas2dStream::static_encode(stream.take_segments(), move(kept_font_ids), move(added_fonts), stream.take_image_frames()));auto video_frame_holds = stream.take_video_frame_holds();if (post_message(encoded_message).is_error()) {did_lose_compositor();diff --git a/Libraries/LibWeb/DOM/Element.cpp b/Libraries/LibWeb/DOM/Element.cppindex 86dff821e87..2fae304e4bf 100644--- a/Libraries/LibWeb/DOM/Element.cpp+++ b/Libraries/LibWeb/DOM/Element.cpp@@ -6942,7 +6942,10 @@ Optional<Gfx::DecodedImageFrame> Element::capture_the_image(){// FIXME: Actually implement this.auto bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, Gfx::IntSize(1, 1)));- return Gfx::DecodedImageFrame { *bitmap };+ auto frame = Gfx::DecodedImageFrame::create(*bitmap);+ if (frame.is_error())+ return {};+ return frame.release_value();}// https://w3c.github.io/pointerevents/#dfn-active-pointerdiff --git a/Libraries/LibWeb/HTML/AnimatedBitmapDecodedImageData.cpp b/Libraries/LibWeb/HTML/AnimatedBitmapDecodedImageData.cppindex df868ee6cde..856226c0700 100644--- a/Libraries/LibWeb/HTML/AnimatedBitmapDecodedImageData.cpp+++ b/Libraries/LibWeb/HTML/AnimatedBitmapDecodedImageData.cpp@@ -27,12 +27,12 @@ GC::Ref<AnimatedBitmapDecodedImageData> AnimatedBitmapDecodedImageData::create(u32 loop_count,Gfx::ColorSpace color_space,Vector<u32> durations,- NonnullRefPtr<Gfx::Bitmap> first_bitmap)+ Gfx::DecodedImageFrame first_frame){auto document_observer = GC::Heap::the().allocate<DOM::DocumentObserver>(document);- auto size = first_bitmap->size();+ auto size = first_frame.size();auto data = GC::Heap::the().allocate<AnimatedBitmapDecodedImageData>(- session_id, durations.size(), loop_count, Gfx::DecodedImageFrame { *first_bitmap, color_space }, document_observer);+ session_id, durations.size(), loop_count, move(first_frame), document_observer);data->m_image_decoder_client_generation = Platform::ImageCodecPlugin::the().client_generation();@@ -64,7 +64,7 @@ AnimatedBitmapDecodedImageData::AnimatedBitmapDecodedImageData(, m_frame_count(frame_count), m_loop_count(loop_count), m_first_frame(move(first_frame))- , m_default_frame(m_first_frame.bitmap_ref(), m_first_frame.color_space())+ , m_default_frame(m_first_frame.with_new_id()){}@@ -72,7 +72,7 @@ AnimatedBitmapDecodedImageData::~AnimatedBitmapDecodedImageData() = default;size_t AnimatedBitmapDecodedImageData::external_memory_size() const{- return m_first_frame.bitmap().data_size();+ return m_first_frame.size_in_bytes();}void AnimatedBitmapDecodedImageData::finalize()diff --git a/Libraries/LibWeb/HTML/AnimatedBitmapDecodedImageData.h b/Libraries/LibWeb/HTML/AnimatedBitmapDecodedImageData.hindex ce1808689b0..2743fd24bad 100644--- a/Libraries/LibWeb/HTML/AnimatedBitmapDecodedImageData.h+++ b/Libraries/LibWeb/HTML/AnimatedBitmapDecodedImageData.h@@ -30,7 +30,7 @@ public:u32 loop_count,Gfx::ColorSpace,Vector<u32> durations,- NonnullRefPtr<Gfx::Bitmap> first_bitmap);+ Gfx::DecodedImageFrame first_frame);virtual ~AnimatedBitmapDecodedImageData() override;virtual void finalize() override;diff --git a/Libraries/LibWeb/HTML/BitmapDecodedImageData.cpp b/Libraries/LibWeb/HTML/BitmapDecodedImageData.cppindex 9d41ccf6740..3189cf3c481 100644--- a/Libraries/LibWeb/HTML/BitmapDecodedImageData.cpp+++ b/Libraries/LibWeb/HTML/BitmapDecodedImageData.cpp@@ -49,7 +49,7 @@ ErrorOr<GC::Ref<BitmapDecodedImageData>> BitmapDecodedImageData::create(Vector<Fif (frames.is_empty())return Error::from_string_literal("Bitmap image has no frames");// NB: Only the first frame is kept, so only an image that has just the one can be decoded again.- if (frames.size() != 1 || frames[0].frame.bitmap().data_size() < minimum_discardable_frame_size) {+ if (frames.size() != 1 || frames[0].frame.size_in_bytes() < minimum_discardable_frame_size) {encoded_data.clear();} else if (encoded_data.capacity() > encoded_data.size()) {// NB: The encoded data is kept for as long as the image, so it should not keep the room it was received into.@@ -85,7 +85,7 @@ void BitmapDecodedImageData::leave_discardable_frames() const{if (!m_discardable_frame_list_node.is_in_list())return;- s_discardable_frame_bytes -= m_frame->bitmap().data_size();+ s_discardable_frame_bytes -= m_frame->size_in_bytes();m_discardable_frame_list_node.remove();}@@ -94,7 +94,7 @@ void BitmapDecodedImageData::did_decode_frame() constVERIFY(m_frame.has_value());VERIFY(!m_discardable_frame_list_node.is_in_list());discardable_frames().append(const_cast<BitmapDecodedImageData&>(*this));- s_discardable_frame_bytes += m_frame->bitmap().data_size();+ s_discardable_frame_bytes += m_frame->size_in_bytes();if (s_discardable_frame_bytes > discardable_frame_budget)schedule_discard_of_idle_frames(frame_idle_time_before_discard);}@@ -132,10 +132,10 @@ void BitmapDecodedImageData::discard_idle_frames()has_frames_that_become_idle_later = true;break;}- // NB: The frame of an image that may be painted is needed by the next paint, which would decode it again. A- // bitmap that something else still holds, e.g. a display list or a pending texture upload, would stay in+ // NB: The frame of an image that may be painted is needed by the next paint, which would decode it again.+ // Pixels that something else still holds, e.g. a display list or a pending texture upload, would stay in// memory. Discarding either would save nothing.- if (image_data.has_clients_that_may_render() || image_data.m_frame->bitmap().ref_count() > 1) {+ if (image_data.has_clients_that_may_render() || image_data.m_frame->pixels_are_held_elsewhere()) {has_held_frames = true;continue;}@@ -196,9 +196,16 @@ Optional<Gfx::DecodedImageFrame> BitmapDecodedImageData::ensure_frame() constreturn {};}- m_failed_attempts_to_decode_again = 0;// NB: The pixels are those of the discarded frame, so the frame keeps its id, by which caches may still hold it.- m_frame = Gfx::DecodedImageFrame::with_id(m_frame_id, move(decoded.bitmap), move(decoded.color_space));+ auto frame_or_error = Gfx::DecodedImageFrame::create_with_id(m_frame_id, *decoded.bitmap, move(decoded.color_space));+ if (frame_or_error.is_error()) {+ dbgln("BitmapDecodedImageData: Could not hold frame decoded again: {}", frame_or_error.error());+ did_fail_to_decode_again();+ return {};+ }++ m_failed_attempts_to_decode_again = 0;+ m_frame = frame_or_error.release_value();did_decode_frame();return m_frame;}@@ -233,7 +240,7 @@ size_t BitmapDecodedImageData::external_memory_size() const{size_t size = m_encoded_data.capacity();if (m_frame.has_value())- size = JS::saturating_add_external_memory_size(size, m_frame->bitmap().data_size());+ size = JS::saturating_add_external_memory_size(size, m_frame->size_in_bytes());return size;}@@ -269,7 +276,7 @@ Optional<Gfx::Color> BitmapDecodedImageData::color_if_single_pixel_bitmap() consauto frame = ensure_frame();if (!frame.has_value())return {};- return frame->bitmap().get_pixel(0, 0);+ return frame->bitmap_sharing_pixels()->get_pixel(0, 0);}Optional<Painting::ImagePaint> BitmapDecodedImageData::image_paint(Painting::ImagePaintRequest const&) constdiff --git a/Libraries/LibWeb/HTML/Canvas/Canvas2DContextBase.cpp b/Libraries/LibWeb/HTML/Canvas/Canvas2DContextBase.cppindex 28100359ae6..fd1ac2db445 100644--- a/Libraries/LibWeb/HTML/Canvas/Canvas2DContextBase.cpp+++ b/Libraries/LibWeb/HTML/Canvas/Canvas2DContextBase.cpp@@ -700,15 +700,17 @@ void web_canvas_2d_style_value_unref(CSS::StyleValue const* style_value)Gfx::DecodedImageFrame* web_canvas_2d_image_frame_from_pixels(u8 const* first_row, size_t pitch, i32 width, i32 height, u8 color_space){- // NB: The bitmap is shareable, so that flushing to the Compositor passes the pixels by file descriptor instead of- // copying them again.+ // NB: The bitmap is shareable, so that the frame shares its memory instead of copying the pixels again.auto bitmap = Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::RGBA8888, Gfx::AlphaType::Unpremultiplied, { width, height });if (bitmap.is_error())return nullptr;auto row_size = static_cast<size_t>(width) * sizeof(Gfx::RawPixel);for (int y = 0; y < height; ++y)__builtin_memcpy(bitmap.value()->scanline(y), first_row + static_cast<size_t>(y) * pitch, row_size);- return new Gfx::DecodedImageFrame { *bitmap.release_value(), Gfx::AlphaType::Unpremultiplied, gfx_color_space_for(predefined_color_space_from_underlying(color_space)) };+ auto frame = Gfx::DecodedImageFrame::create(*bitmap.release_value(), gfx_color_space_for(predefined_color_space_from_underlying(color_space)));+ if (frame.is_error())+ return nullptr;+ return new Gfx::DecodedImageFrame { frame.release_value() };}static CanvasImageSource image_source_from(void* source_wrapper, CanvasImageSourceKind kind)diff --git a/Libraries/LibWeb/HTML/Canvas/CanvasHost.cpp b/Libraries/LibWeb/HTML/Canvas/CanvasHost.cppindex 0d8e10e863d..e002627a653 100644--- a/Libraries/LibWeb/HTML/Canvas/CanvasHost.cpp+++ b/Libraries/LibWeb/HTML/Canvas/CanvasHost.cpp@@ -46,6 +46,14 @@ static RefPtr<Gfx::Bitmap> create_transparent_canvas_bitmap(Gfx::IntSize size)return bitmap_or_error.release_value();}+static Optional<Gfx::DecodedImageFrame> frame_of_bitmap(Gfx::Bitmap const& bitmap, Gfx::ColorSpace color_space = {})+{+ auto frame = Gfx::DecodedImageFrame::create(bitmap, move(color_space));+ if (frame.is_error())+ return {};+ return frame.release_value();+}+RefPtr<Gfx::Bitmap> CanvasHost::get_bitmap_from_surface(){auto const size = bitmap_size_for_canvas();@@ -56,7 +64,7 @@ RefPtr<Gfx::Bitmap> CanvasHost::get_bitmap_from_surface()if (auto* context = canvas_bitmap_renderer_context(); context && !context->is_blank()) {// FIXME: Convert bitmap data that isn't in the sRGB color space.if (auto const* frame = context->frame(); frame) {- if (auto bitmap = frame->bitmap().clone(); !bitmap.is_error())+ if (auto bitmap = frame->bitmap_sharing_pixels()->clone(); !bitmap.is_error())return bitmap.release_value();}return nullptr;@@ -91,14 +99,14 @@ Optional<Gfx::DecodedImageFrame> CanvasHost::get_frame_from_surface()context->ensure_backing_storage();auto color_space = context->color_space();if (auto pixels = context->read_pixels({ {}, size }, Gfx::AlphaType::Premultiplied, color_space); pixels && pixels->size() == size)- return Gfx::DecodedImageFrame { *pixels, gfx_color_space_for(color_space) };+ return frame_of_bitmap(*pixels, gfx_color_space_for(color_space));return {};}auto bitmap = get_bitmap_from_surface();if (!bitmap)return {};- return Gfx::DecodedImageFrame { *bitmap };+ return frame_of_bitmap(*bitmap);}Optional<Gfx::DecodedImageFrame> CanvasHost::get_frame_for_serialization(Utf16View type)@@ -108,7 +116,7 @@ Optional<Gfx::DecodedImageFrame> CanvasHost::get_frame_for_serialization(Utf16Viauto bitmap = get_bitmap_from_surface();if (!bitmap)return {};- return Gfx::DecodedImageFrame { *bitmap };+ return frame_of_bitmap(*bitmap);}// https://html.spec.whatwg.org/multipage/canvas.html#concept-canvas-origin-cleandiff --git a/Libraries/LibWeb/HTML/Canvas/RemoteCanvas2DTransportFFI.cpp b/Libraries/LibWeb/HTML/Canvas/RemoteCanvas2DTransportFFI.cppindex e26b7b0163f..8ce28ab5ef9 100644--- a/Libraries/LibWeb/HTML/Canvas/RemoteCanvas2DTransportFFI.cpp+++ b/Libraries/LibWeb/HTML/Canvas/RemoteCanvas2DTransportFFI.cpp@@ -154,12 +154,15 @@ bool web_canvas_2d_bitmap_unpremultiplied_rgba_pixels(void* bitmap, u8 const** oreturn true;}-// A Gfx::DecodedImageFrame of the bitmap, whose pixels are in the color space, allocated with new, which takes over the-// reference to the bitmap.+// A Gfx::DecodedImageFrame of the pixels of the bitmap, which are in the color space, allocated with new, or null if+// one cannot be made. The reference to the bitmap is taken over either way.void* web_canvas_2d_image_frame_from_bitmap(void* bitmap, u8 color_space){NonnullRefPtr<Gfx::Bitmap const> adopted_bitmap = adopt_ref(*static_cast<Gfx::Bitmap*>(bitmap));- return new Gfx::DecodedImageFrame { move(adopted_bitmap), gfx_color_space_for(predefined_color_space_from_underlying(color_space)) };+ auto frame = Gfx::DecodedImageFrame::create(*adopted_bitmap, gfx_color_space_for(predefined_color_space_from_underlying(color_space)));+ if (frame.is_error())+ return nullptr;+ return new Gfx::DecodedImageFrame { frame.release_value() };}}diff --git a/Libraries/LibWeb/HTML/Canvas/SerializeBitmap.cpp b/Libraries/LibWeb/HTML/Canvas/SerializeBitmap.cppindex 8c42abbefd8..32cf3db1673 100644--- a/Libraries/LibWeb/HTML/Canvas/SerializeBitmap.cpp+++ b/Libraries/LibWeb/HTML/Canvas/SerializeBitmap.cpp@@ -51,7 +51,7 @@ bool serialization_of_the_bitmap_supports_color_profiles(Utf16View type)// https://html.spec.whatwg.org/multipage/canvas.html#a-serialisation-of-the-bitmap-as-a-fileErrorOr<SerializeBitmapResult> serialize_bitmap(Gfx::DecodedImageFrame const& frame, Utf16View type, Optional<double> quality){- auto const& bitmap = frame.bitmap();+ auto bitmap = frame.bitmap_sharing_pixels();Optional<ReadonlyBytes> icc_data;Optional<Media::CodingIndependentCodePoints> cicp;frame.color_space().data().visit(@@ -78,13 +78,13 @@ ErrorOr<SerializeBitmapResult> serialize_bitmap(Gfx::DecodedImageFrame const& frjpeg_options.icc_data = icc_data;if (valid_quality)jpeg_options.quality = static_cast<int>(quality.value() * 100);- TRY(Gfx::JPEGWriter::encode(file, bitmap, jpeg_options));+ TRY(Gfx::JPEGWriter::encode(file, *bitmap, jpeg_options));return SerializeBitmapResult { TRY(file.read_until_eof()), SerializedBitmapMimeType::JPEG };}// User agents must support PNG ("image/png"). User agents may support other types.// If the user agent does not support the requested type, then it must create the file using the PNG format. [PNG]- return SerializeBitmapResult { TRY(Gfx::PNGWriter::encode(bitmap, { .icc_data = icc_data, .cicp = cicp })), SerializedBitmapMimeType::PNG };+ return SerializeBitmapResult { TRY(Gfx::PNGWriter::encode(*bitmap, { .icc_data = icc_data, .cicp = cicp })), SerializedBitmapMimeType::PNG };}}diff --git a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp b/Libraries/LibWeb/HTML/HTMLLinkElement.cppindex ec172f82b21..0468bf052a6 100644--- a/Libraries/LibWeb/HTML/HTMLLinkElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLLinkElement.cpp@@ -1427,7 +1427,7 @@ static NonnullRefPtr<Core::Promise<NonnullRefPtr<Gfx::Bitmap const>>> decode_favpromise->reject(Error::from_string_view("Failed to get bitmap from SVG favicon"sv));return;}- promise->resolve(decoded_frame->bitmap_ref());+ promise->resolve(decoded_frame->bitmap_sharing_pixels());}).when_rejected([promise](Error& error) {promise->reject(move(error));diff --git a/Libraries/LibWeb/HTML/HTMLVideoElement.cpp b/Libraries/LibWeb/HTML/HTMLVideoElement.cppindex 741e91928d6..c7555567301 100644--- a/Libraries/LibWeb/HTML/HTMLVideoElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLVideoElement.cpp@@ -272,8 +272,10 @@ WebIDL::ExceptionOr<void> HTMLVideoElement::determine_element_poster_frame(Optiostatic constexpr auto finalize = [](HTMLVideoElement& self, RefPtr<Gfx::Bitmap> poster_frame) {self.m_poster_frame.clear();- if (poster_frame)- self.m_poster_frame = Gfx::DecodedImageFrame { *poster_frame };+ if (poster_frame) {+ if (auto frame = Gfx::DecodedImageFrame::create(*poster_frame); !frame.is_error())+ self.m_poster_frame = frame.release_value();+ }self.m_load_event_delayer.clear();self.m_fetch_controller = nullptr;self.update_representation();diff --git a/Libraries/LibWeb/HTML/ImageBitmap.cpp b/Libraries/LibWeb/HTML/ImageBitmap.cppindex 131304ba672..5a3afc4b49a 100644--- a/Libraries/LibWeb/HTML/ImageBitmap.cpp+++ b/Libraries/LibWeb/HTML/ImageBitmap.cpp@@ -35,7 +35,7 @@ static constexpr size_t shared_pixels_alignment = 64;return {};}-[[nodiscard]] static WebIDL::ExceptionOr<NonnullRefPtr<Gfx::Bitmap>> create_bitmap_from_bitmap_data(JS::VM& vm, Gfx::BitmapFormat const format, Gfx::AlphaType const alpha_type, int const width, int const height, size_t const pitch, ByteBuffer data)+[[nodiscard]] static WebIDL::ExceptionOr<NonnullRefPtr<Gfx::Bitmap const>> bitmap_borrowing_bitmap_data(JS::VM& vm, Gfx::BitmapFormat const format, Gfx::AlphaType const alpha_type, int const width, int const height, size_t const pitch, ReadonlyBytes data){TRY(validate_bitmap_layout(format, alpha_type, width, height));auto required_size = Checked<size_t> { pitch };@@ -43,9 +43,8 @@ static constexpr size_t shared_pixels_alignment = 64;if (pitch < Gfx::Bitmap::minimum_pitch(width, format) || required_size.has_overflow() || required_size.value() > data.size())return WebIDL::DataCloneError::create("Invalid ImageBitmap pixel buffer"_utf16);- auto bitmap_data = TRY_OR_THROW_OOM(vm, try_make<ByteBuffer>(move(data)));- auto* pixels = bitmap_data->data();- return TRY_OR_THROW_OOM(vm, Gfx::Bitmap::create_wrapper(format, alpha_type, Gfx::IntSize(width, height), pitch, pixels, [bitmap_data = move(bitmap_data)] { }));+ NonnullRefPtr<Gfx::Bitmap const> bitmap = TRY_OR_THROW_OOM(vm, Gfx::Bitmap::create_wrapper(format, alpha_type, Gfx::IntSize(width, height), pitch, const_cast<u8*>(data.data())));+ return bitmap;}enum class SerializedColorSpaceKind : u8 {@@ -54,13 +53,18 @@ enum class SerializedColorSpaceKind : u8 {ICCProfile,};-[[nodiscard]] static WebIDL::ExceptionOr<NonnullRefPtr<Gfx::Bitmap>> bitmap_from_shared_pixels(JS::VM& vm, Vector<Core::AnonymousBuffer> const* shared_buffers, u32 const buffer_index, u64 const offset, Gfx::BitmapFormat const format, Gfx::AlphaType const alpha_type, int const width, int const height)+struct SharedPixelsOfFrame {+ Core::AnonymousBuffer shared_memory;+ size_t offset_of_pixels { 0 };+};++[[nodiscard]] static WebIDL::ExceptionOr<SharedPixelsOfFrame> shared_pixels_of_frame(ReceivedImageBitmapSharedPixels* received_shared_pixels, u32 const buffer_index, u64 const offset, Gfx::BitmapFormat const format, Gfx::AlphaType const alpha_type, int const width, int const height){TRY(validate_bitmap_layout(format, alpha_type, width, height));- if (!shared_buffers || buffer_index >= shared_buffers->size())+ if (!received_shared_pixels || buffer_index >= received_shared_pixels->buffers.size())return WebIDL::DataCloneError::create("Invalid ImageBitmap shared pixels"_utf16);- auto buffer = (*shared_buffers)[buffer_index];+ auto buffer = received_shared_pixels->buffers[buffer_index];auto const pitch = Gfx::Bitmap::minimum_pitch(width, format);auto const pixel_size = Gfx::Bitmap::size_in_bytes(pitch, height);@@ -77,36 +81,25 @@ enum class SerializedColorSpaceKind : u8 {return WebIDL::DataCloneError::create("Unsealed ImageBitmap shared pixels"_utf16);if (buffer.validate_backing_size().is_error())return WebIDL::DataCloneError::create("Invalid ImageBitmap shared pixels"_utf16);+ return SharedPixelsOfFrame { move(buffer), static_cast<size_t>(offset) };#else- // NB: Without resize seals, snapshot just this bitmap through an operation that reports truncation instead of- // faulting. Copying the entire shared buffer for every bitmap would make batch transfers quadratic.- auto snapshot = buffer.snapshot(static_cast<size_t>(offset), pixel_size);- if (snapshot.is_error())- return WebIDL::DataCloneError::create("Failed to snapshot ImageBitmap shared pixels"_utf16);- if (offset == 0 && buffer.size() == round_up_to_power_of_two(pixel_size, shared_pixels_alignment))- return TRY_OR_THROW_OOM(vm, Gfx::Bitmap::create_with_anonymous_buffer(format, alpha_type, snapshot.release_value(), { width, height }));-- // NB: A batch must not retain a separate snapshot descriptor for every bitmap. Copy the independent pixels into- // ordinary bitmap storage, releasing each temporary descriptor before receiving the next bitmap.- auto bitmap = TRY_OR_THROW_OOM(vm, Gfx::Bitmap::create(format, alpha_type, { width, height }));- snapshot.value().bytes().copy_to({ bitmap->scanline_u8(0), pixel_size });- return bitmap;+ // NB: Without resize seals, snapshot the buffer through an operation that reports truncation instead of faulting.+ // Every bitmap in the buffer reads from the one snapshot. Copying the entire shared buffer for every bitmap+ // would make batch transfers quadratic, and a snapshot of each bitmap would keep a descriptor for each.+ auto& snapshot = received_shared_pixels->snapshots_of_buffers_without_resize_seals[buffer_index];+ if (!snapshot.has_value()) {+ auto snapshot_or_error = buffer.snapshot();+ if (snapshot_or_error.is_error())+ return WebIDL::DataCloneError::create("Failed to snapshot ImageBitmap shared pixels"_utf16);+ snapshot = snapshot_or_error.release_value();+ }+ return SharedPixelsOfFrame { *snapshot, static_cast<size_t>(offset) };#endif-- // NB: A buffer that holds just this bitmap backs it directly. Passing the bitmap on, as to WebGL or the compositor,- // then shares the memory rather than copying it.- Gfx::IntSize const size { width, height };- if (offset == 0 && buffer.size() == round_up_to_power_of_two(Gfx::Bitmap::size_in_bytes(pitch, height), shared_pixels_alignment))- return TRY_OR_THROW_OOM(vm, Gfx::Bitmap::create_with_anonymous_buffer(format, alpha_type, move(buffer), size));-- auto* pixels = buffer.data<u8>() + offset;- return TRY_OR_THROW_OOM(vm, Gfx::Bitmap::create_wrapper(format, alpha_type, size, pitch, pixels, [buffer = move(buffer)] { }));}-size_t ImageBitmap::shared_pixels_size(Gfx::Bitmap const& bitmap)+size_t ImageBitmap::shared_pixels_size(Gfx::DecodedImageFrame const& frame){- auto size = Gfx::Bitmap::size_in_bytes(Gfx::Bitmap::minimum_pitch(bitmap.width(), bitmap.format()), bitmap.height());- return round_up_to_power_of_two(size, shared_pixels_alignment);+ return round_up_to_power_of_two(frame.size_in_bytes(), shared_pixels_alignment);}// Hands a decoded frame over to the Rust implementation, which takes it over, or null for none.@@ -146,17 +139,17 @@ struct FramePixels {size_t icc_profile_size { 0 };};-// What Bindings/Rust/src/html/image_bitmap.rs asks of C++, which owns the encoding of the Gfx::Bitmap of a transferred+// What Bindings/Rust/src/html/image_bitmap.rs asks of C++, which owns the encoding of the pixels of a transferred// frame: a frame that it is handed over is a Gfx::DecodedImageFrame allocated with new, or null for none.size_t web_image_bitmap_frame_size_in_bytes(Gfx::DecodedImageFrame const*);void web_image_bitmap_frame_pixels(Gfx::DecodedImageFrame const*, FramePixels*);JSCompletion web_image_bitmap_create_frame(void* realm, FramePixels const*, Gfx::DecodedImageFrame** out_frame);bool web_image_bitmap_copy_to_shared_pixels(ImageBitmapSharedPixels*, Gfx::DecodedImageFrame const*, u32* buffer_index, u64* offset);-JSCompletion web_image_bitmap_create_frame_from_shared_pixels(void* realm, Vector<Core::AnonymousBuffer> const*, u32 buffer_index, u64 offset, FramePixels const*, Gfx::DecodedImageFrame** out_frame);+JSCompletion web_image_bitmap_create_frame_from_shared_pixels(void* realm, ReceivedImageBitmapSharedPixels*, u32 buffer_index, u64 offset, FramePixels const*, Gfx::DecodedImageFrame** out_frame);size_t web_image_bitmap_frame_size_in_bytes(Gfx::DecodedImageFrame const* frame){- return frame->bitmap().data_size();+ return frame->size_in_bytes();}// NB: The Rust side names these values by their underlying values.@@ -172,14 +165,13 @@ static_assert(to_underlying(SerializedColorSpaceKind::ICCProfile) == 2);void web_image_bitmap_frame_pixels(Gfx::DecodedImageFrame const* frame, FramePixels* pixels){- auto const& bitmap = frame->bitmap();- pixels->width = bitmap.width();- pixels->height = bitmap.height();- pixels->pitch = bitmap.pitch();- pixels->format = to_underlying(bitmap.format());- pixels->alpha_type = to_underlying(bitmap.alpha_type());- pixels->data = bitmap.scanline_u8(0);- pixels->data_size = bitmap.data_size();+ pixels->width = frame->width();+ pixels->height = frame->height();+ pixels->pitch = frame->pitch();+ pixels->format = to_underlying(frame->format());+ pixels->alpha_type = to_underlying(frame->alpha_type());+ pixels->data = frame->pixels().data();+ pixels->data_size = frame->size_in_bytes();frame->color_space().data().visit([&](Empty) {pixels->color_space_kind = to_underlying(SerializedColorSpaceKind::SRGB);@@ -218,12 +210,14 @@ JSCompletion web_image_bitmap_create_frame(void* realm, FramePixels const* pixel{auto& js_realm = *static_cast<JS::Realm*>(realm);auto frame = [&]() -> WebIDL::ExceptionOr<Gfx::DecodedImageFrame> {- auto data = TRY_OR_THROW_OOM(js_realm.vm(), ByteBuffer::copy(pixels->data, pixels->data_size));- auto bitmap = TRY(create_bitmap_from_bitmap_data(js_realm.vm(), static_cast<Gfx::BitmapFormat>(pixels->format), static_cast<Gfx::AlphaType>(pixels->alpha_type), pixels->width, pixels->height, pixels->pitch, move(data)));+ auto bitmap = TRY(bitmap_borrowing_bitmap_data(js_realm.vm(), static_cast<Gfx::BitmapFormat>(pixels->format), static_cast<Gfx::AlphaType>(pixels->alpha_type), pixels->width, pixels->height, pixels->pitch, { pixels->data, pixels->data_size }));auto color_space = color_space_from_frame_pixels(*pixels);if (color_space.is_error())return data_clone_error_from_serialization_error(js_realm, color_space.release_error());- return Gfx::DecodedImageFrame { *bitmap, color_space.release_value() };+ auto frame = Gfx::DecodedImageFrame::create(*bitmap, color_space.release_value());+ if (frame.is_error())+ return data_clone_error_from_serialization_error(js_realm, frame.release_error());+ return frame.release_value();}();if (frame.is_exception())return Bindings::completion_to_abi(js_realm, frame.release_error());@@ -235,31 +229,32 @@ JSCompletion web_image_bitmap_create_frame(void* realm, FramePixels const* pixel// returns where they are.bool web_image_bitmap_copy_to_shared_pixels(ImageBitmapSharedPixels* shared_pixels, Gfx::DecodedImageFrame const* frame, u32* buffer_index, u64* offset){- auto const& bitmap = frame->bitmap();- if (!shared_pixels || ImageBitmap::shared_pixels_size(bitmap) > shared_pixels->buffer.size() - shared_pixels->used_size)+ if (!shared_pixels || ImageBitmap::shared_pixels_size(*frame) > shared_pixels->buffer.size() - shared_pixels->used_size)return false;- auto const pitch = Gfx::Bitmap::minimum_pitch(bitmap.width(), bitmap.format());- auto* pixels = shared_pixels->buffer.data<u8>() + shared_pixels->used_size;- for (int y = 0; y < bitmap.height(); ++y)- memcpy(pixels + y * pitch, bitmap.scanline_u8(y), pitch);+ frame->pixels().copy_to({ shared_pixels->buffer.data<u8>() + shared_pixels->used_size, frame->size_in_bytes() });*buffer_index = shared_pixels->buffer_index;*offset = shared_pixels->used_size;- shared_pixels->used_size += ImageBitmap::shared_pixels_size(bitmap);+ shared_pixels->used_size += ImageBitmap::shared_pixels_size(*frame);return true;}// AD-HOC: A frame over the pixels at the offset of the shared memory of a transfer, as the transfer steps of// Bindings/Rust/src/html/image_bitmap.rs place them there. The values of the bitmap and the color space are in// the pixels, whose bytes are unused.-JSCompletion web_image_bitmap_create_frame_from_shared_pixels(void* realm, Vector<Core::AnonymousBuffer> const* shared_buffers, u32 buffer_index, u64 offset, FramePixels const* pixels, Gfx::DecodedImageFrame** out_frame)+JSCompletion web_image_bitmap_create_frame_from_shared_pixels(void* realm, ReceivedImageBitmapSharedPixels* received_shared_pixels, u32 buffer_index, u64 offset, FramePixels const* pixels, Gfx::DecodedImageFrame** out_frame){auto& js_realm = *static_cast<JS::Realm*>(realm);auto frame = [&]() -> WebIDL::ExceptionOr<Gfx::DecodedImageFrame> {- auto bitmap = TRY(bitmap_from_shared_pixels(js_realm.vm(), shared_buffers, buffer_index, offset, static_cast<Gfx::BitmapFormat>(pixels->format), static_cast<Gfx::AlphaType>(pixels->alpha_type), pixels->width, pixels->height));+ auto format = static_cast<Gfx::BitmapFormat>(pixels->format);+ auto alpha_type = static_cast<Gfx::AlphaType>(pixels->alpha_type);+ auto shared_pixels = TRY(shared_pixels_of_frame(received_shared_pixels, buffer_index, offset, format, alpha_type, pixels->width, pixels->height));auto color_space = color_space_from_frame_pixels(*pixels);if (color_space.is_error())return data_clone_error_from_serialization_error(js_realm, color_space.release_error());- return Gfx::DecodedImageFrame { *bitmap, color_space.release_value() };+ auto frame = Gfx::DecodedImageFrame::create_over_shared_memory(move(shared_pixels.shared_memory), shared_pixels.offset_of_pixels, { pixels->width, pixels->height }, format, alpha_type, color_space.release_value());+ if (frame.is_error())+ return data_clone_error_from_serialization_error(js_realm, frame.release_error());+ return frame.release_value();}();if (frame.is_exception())return Bindings::completion_to_abi(js_realm, frame.release_error());@@ -271,8 +266,10 @@ JSCompletion web_image_bitmap_create_frame_from_shared_pixels(void* realm, VectoGC::Ref<ImageBitmap> ImageBitmap::create(RefPtr<Gfx::Bitmap const> bitmap){Optional<Gfx::DecodedImageFrame> frame;- if (bitmap)- frame = Gfx::DecodedImageFrame { bitmap.release_nonnull() };+ if (bitmap) {+ if (auto frame_or_error = Gfx::DecodedImageFrame::create(*bitmap); !frame_or_error.is_error())+ frame = frame_or_error.release_value();+ }return create(move(frame));}@@ -317,11 +314,4 @@ Gfx::DecodedImageFrame const* ImageBitmap::frame() constreturn web_image_bitmap_frame(this);}-Gfx::Bitmap const* ImageBitmap::bitmap() const-{- if (auto const* frame = web_image_bitmap_frame(this))- return &frame->bitmap();- return nullptr;-}-}diff --git a/Libraries/LibWeb/HTML/ImageBitmap.h b/Libraries/LibWeb/HTML/ImageBitmap.hindex b0f1ce0334c..309a15cfab6 100644--- a/Libraries/LibWeb/HTML/ImageBitmap.h+++ b/Libraries/LibWeb/HTML/ImageBitmap.h@@ -9,6 +9,7 @@#include <AK/Optional.h>#include <AK/RefPtr.h>#include <AK/Types.h>+#include <AK/Vector.h>#include <LibCore/AnonymousBuffer.h>#include <LibGfx/DecodedImageFrame.h>#include <LibGfx/Forward.h>@@ -39,6 +40,13 @@ struct ImageBitmapSharedPixels {size_t used_size { 0 };};+struct ReceivedImageBitmapSharedPixels {+ AK_ALLOC_WITH_KMALLOC;++ Vector<Core::AnonymousBuffer> buffers;+ Vector<Optional<Core::AnonymousBuffer>> snapshots_of_buffers_without_resize_seals;+};+// https://html.spec.whatwg.org/multipage/imagebitmap-and-animations.html#imagebitmap// The C++ side of an ImageBitmap, which is implemented in Rust (Libraries/LibWeb/Bindings/Rust/src/html): what// createImageBitmap(), OffscreenCanvas and WebDriver create, and createImageBitmap() and StructuredSerialize read.@@ -66,12 +74,10 @@ public:bool is_origin_clean() const;void set_origin_clean(bool);- // The bitmap of the bitmap data, which the ImageBitmap keeps until it is closed or transferred.- Gfx::Bitmap const* bitmap() const;- // The bitmap data with its color space.+ // The bitmap data with its color space, which the ImageBitmap keeps until it is closed or transferred.Gfx::DecodedImageFrame const* frame() const;- static size_t shared_pixels_size(Gfx::Bitmap const&);+ static size_t shared_pixels_size(Gfx::DecodedImageFrame const&);};}diff --git a/Libraries/LibWeb/HTML/SharedResourceRequest.cpp b/Libraries/LibWeb/HTML/SharedResourceRequest.cppindex 4b83db1b3ff..e882ae78f0e 100644--- a/Libraries/LibWeb/HTML/SharedResourceRequest.cpp+++ b/Libraries/LibWeb/HTML/SharedResourceRequest.cpp@@ -350,19 +350,24 @@ void SharedResourceRequest::handle_successful_fetch(URL::URL const& url_string,auto handle_successful_bitmap_decode = [strong_this = GC::Root(*this), image_data_is_cors_cross_origin, encoded_data = move(encoded_data)](Web::Platform::DecodedImage& result) mutable -> ErrorOr<void> {if (result.session_id != 0) {// Streaming animated decode: create AnimatedBitmapDecodedImageData.+ auto first_frame = Gfx::DecodedImageFrame::create(*result.frames.first().bitmap, result.color_space);+ if (first_frame.is_error()) {+ Web::Platform::ImageCodecPlugin::the().stop_animation_decode(result.session_id);+ return first_frame.release_error();+ }strong_this->m_image_data = AnimatedBitmapDecodedImageData::create(*strong_this->m_document,result.session_id,result.loop_count,move(result.color_space),move(result.all_durations),- *result.frames.first().bitmap);+ first_frame.release_value());} else {// Single-shot decode: create BitmapDecodedImageData as before.Vector<BitmapDecodedImageData::Frame> frames;for (auto& frame : result.frames) {frames.append(BitmapDecodedImageData::Frame {- .frame = Gfx::DecodedImageFrame { *frame.bitmap, result.color_space },+ .frame = TRY(Gfx::DecodedImageFrame::create(*frame.bitmap, result.color_space)),.duration = static_cast<int>(frame.duration),});}diff --git a/Libraries/LibWeb/HTML/StructuredSerializeRustIntegration.cpp b/Libraries/LibWeb/HTML/StructuredSerializeRustIntegration.cppindex 35ff552f260..840d0f6e639 100644--- a/Libraries/LibWeb/HTML/StructuredSerializeRustIntegration.cpp+++ b/Libraries/LibWeb/HTML/StructuredSerializeRustIntegration.cpp@@ -61,8 +61,8 @@ bool web_structured_serialize_validate_shared_memory(int, size_t);SerializedTransferRecord* web_structured_serialize_transfer_record_create(ReadonlyBytes, JSObject* const*, size_t, int const*, size_t const*, size_t, int, TransferDataEncoder* const*, size_t);void web_structured_serialize_transfer_record_destroy(SerializedTransferRecord*);void web_structured_serialize_transfer_record_take(SerializedTransferRecord*, void*, void (*)(void*, ReadonlyBytes, JSObject* const*, size_t, int const*, size_t const*, size_t, int), void (*)(void*, TransferDataEncoder*));-Vector<Core::AnonymousBuffer>* web_structured_serialize_shared_pixels_create(int const*, size_t const*, size_t);-void web_structured_serialize_shared_pixels_destroy(Vector<Core::AnonymousBuffer>*);+ReceivedImageBitmapSharedPixels* web_structured_serialize_shared_pixels_create(int const*, size_t const*, size_t);+void web_structured_serialize_shared_pixels_destroy(ReceivedImageBitmapSharedPixels*);void web_structured_serialize_transferred_values_append(Vector<GC::Root<JS::Object>>*, JSObject*);}@@ -158,8 +158,8 @@ ImageBitmapSharedPixels* web_structured_serialize_gather_image_bitmap_pixels(JSO{Checked<size_t> image_bitmap_pixels_size = 0;for (size_t i = 0; i < count; ++i) {- if (auto* image_bitmap = Bindings::impl_from<ImageBitmap>(JS::HostABI::object_from_abi(transfer_list[i])); image_bitmap && image_bitmap->bitmap())- image_bitmap_pixels_size += ImageBitmap::shared_pixels_size(*image_bitmap->bitmap());+ if (auto* image_bitmap = Bindings::impl_from<ImageBitmap>(JS::HostABI::object_from_abi(transfer_list[i])); image_bitmap && image_bitmap->frame())+ image_bitmap_pixels_size += ImageBitmap::shared_pixels_size(*image_bitmap->frame());}if (image_bitmap_pixels_size.has_overflow() || image_bitmap_pixels_size.value() == 0)return nullptr;@@ -241,7 +241,7 @@ void web_structured_serialize_transfer_record_take(SerializedTransferRecord* recrecord->shared_buffers.clear();}-Vector<Core::AnonymousBuffer>* web_structured_serialize_shared_pixels_create(int const* descriptors, size_t const* sizes, size_t count)+ReceivedImageBitmapSharedPixels* web_structured_serialize_shared_pixels_create(int const* descriptors, size_t const* sizes, size_t count){Vector<Core::AnonymousBuffer> buffers;for (size_t i = 0; i < count; ++i) {@@ -253,12 +253,14 @@ Vector<Core::AnonymousBuffer>* web_structured_serialize_shared_pixels_create(intreturn nullptr;buffers.append(buffer.release_value());}- return new Vector<Core::AnonymousBuffer>(move(buffers));+ auto* received_shared_pixels = new ReceivedImageBitmapSharedPixels { .buffers = move(buffers), .snapshots_of_buffers_without_resize_seals = {} };+ received_shared_pixels->snapshots_of_buffers_without_resize_seals.resize(count);+ return received_shared_pixels;}-void web_structured_serialize_shared_pixels_destroy(Vector<Core::AnonymousBuffer>* buffers)+void web_structured_serialize_shared_pixels_destroy(ReceivedImageBitmapSharedPixels* received_shared_pixels){- delete buffers;+ delete received_shared_pixels;}void web_structured_serialize_transferred_values_append(Vector<GC::Root<JS::Object>>* transferred_values, JSObject* value)diff --git a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cppindex 6c006f638cb..1a4d2a0277b 100644--- a/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp+++ b/Libraries/LibWeb/HTML/WindowOrWorkerGlobalScope.cpp@@ -290,7 +290,7 @@ static ErrorOr<NonnullRefPtr<Gfx::Bitmap>> scale_bitmap(Compositor::CompositorHoauto output = TRY(Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, size));Compositing::DisplayListResourceStorage resource_storage;- auto display_list = Painting::record_image_frame_display_list(Gfx::DecodedImageFrame { input }, output->rect().to_type<float>(), scaling_mode, resource_storage);+ auto display_list = Painting::record_image_frame_display_list(TRY(Gfx::DecodedImageFrame::create(input)), output->rect().to_type<float>(), scaling_mode, resource_storage);if (!compositor_host->rasterize_display_list(display_list, resource_storage, output))return Error::from_string_literal("Unable to scale the bitmap");return output;@@ -456,7 +456,7 @@ static ErrorOr<Gfx::DecodedImageFrame> crop_to_the_source_rectangle_with_formatt// FIXME: 10. Let val be the value of premultiplyAlpha member of options, and then run these substeps:// 11. Return output.- return Gfx::DecodedImageFrame { *output, move(output_color_space) };+ return Gfx::DecodedImageFrame::create(*output, move(output_color_space));}struct VectorImageRaster {@@ -699,7 +699,7 @@ void WindowOrWorkerGlobalScopeMixin::create_image_bitmap_impl(JS::Realm& realm,WebIDL::reject_promise(*p, WebIDL::InvalidStateError::create("Image size is invalid"_utf16));return;}- auto cropped_bitmap_or_error = crop_to_the_source_rectangle_with_formatting(compositor_host_for(realm.global_object()), canvas_frame->bitmap(), canvas_frame->color_space(), sx, sy, sw, sh, options);+ auto cropped_bitmap_or_error = crop_to_the_source_rectangle_with_formatting(compositor_host_for(realm.global_object()), *canvas_frame->bitmap_sharing_pixels(), canvas_frame->color_space(), sx, sy, sw, sh, options);// AD-HOC: Reject promise with an "InvalidStateError" DOMException on allocation failure// Spec issue: https://github.com/whatwg/html/issues/3323if (cropped_bitmap_or_error.is_error()) {@@ -728,7 +728,7 @@ void WindowOrWorkerGlobalScopeMixin::create_image_bitmap_impl(JS::Realm& realm,}// 1. Set imageBitmap's bitmap data to a copy of image's bitmap data, cropped to the source rectangle with formatting.- auto cropped_bitmap_or_error = crop_to_the_source_rectangle_with_formatting(compositor_host_for(realm.global_object()), source_image_bitmap->frame()->bitmap(), source_image_bitmap->frame()->color_space(), sx, sy, sw, sh, options);+ auto cropped_bitmap_or_error = crop_to_the_source_rectangle_with_formatting(compositor_host_for(realm.global_object()), *source_image_bitmap->frame()->bitmap_sharing_pixels(), source_image_bitmap->frame()->color_space(), sx, sy, sw, sh, options);// AD-HOC: Reject promise with an "InvalidStateError" DOMException on allocation failure// Spec issue: https://github.com/whatwg/html/issues/3323if (cropped_bitmap_or_error.is_error()) {@@ -766,7 +766,7 @@ void WindowOrWorkerGlobalScopeMixin::create_image_bitmap_impl(JS::Realm& realm,WebIDL::reject_promise(*p, WebIDL::InvalidStateError::create("Video has no frame"_utf16));return;}- auto cropped_bitmap_or_error = crop_to_the_source_rectangle_with_formatting(compositor_host_for(realm.global_object()), video_frame->bitmap(), video_frame->color_space(), sx, sy, sw, sh, options);+ auto cropped_bitmap_or_error = crop_to_the_source_rectangle_with_formatting(compositor_host_for(realm.global_object()), *video_frame->bitmap_sharing_pixels(), video_frame->color_space(), sx, sy, sw, sh, options);// AD-HOC: Reject promise with an "InvalidStateError" DOMException on allocation failure// Spec issue: https://github.com/whatwg/html/issues/3323if (cropped_bitmap_or_error.is_error()) {@@ -827,7 +827,7 @@ void WindowOrWorkerGlobalScopeMixin::create_image_bitmap_impl(JS::Realm& realm,WebIDL::reject_promise(*p, WebIDL::InvalidStateError::create("Image could not be rendered"_utf16));return;}- auto cropped_bitmap_or_error = crop_to_the_source_rectangle_with_formatting(compositor_host_for(realm.global_object()), decoded_frame->bitmap(), decoded_frame->color_space(), sx, sy, sw, sh, options, ImageIsImageElementOrBlob::Yes);+ auto cropped_bitmap_or_error = crop_to_the_source_rectangle_with_formatting(compositor_host_for(realm.global_object()), *decoded_frame->bitmap_sharing_pixels(), decoded_frame->color_space(), sx, sy, sw, sh, options, ImageIsImageElementOrBlob::Yes);// AD-HOC: Reject promise with an "InvalidStateError" DOMException on allocation failure// Spec issue: https://github.com/whatwg/html/issues/3323if (cropped_bitmap_or_error.is_error()) {diff --git a/Libraries/LibWeb/NotificationsAPI/Notification.cpp b/Libraries/LibWeb/NotificationsAPI/Notification.cppindex 6a76ed5d455..ebfc13b437b 100644--- a/Libraries/LibWeb/NotificationsAPI/Notification.cpp+++ b/Libraries/LibWeb/NotificationsAPI/Notification.cpp@@ -437,10 +437,13 @@ void Notification::fetch_a_notification_resource(URL::URL const& url, GC::Ref<GC// Without a compositor to scale with, the platform gets the icon as it came.if (m_page && m_page->has_compositor_host()) {auto output = Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, scaled_size);- Compositing::DisplayListResourceStorage resource_storage;- auto display_list = Painting::record_image_frame_display_list(Gfx::DecodedImageFrame { *bitmap }, Gfx::FloatRect { {}, scaled_size.to_type<float>() }, Gfx::ScalingMode::BilinearMipmap, resource_storage);- if (!output.is_error() && m_page->compositor_host().rasterize_display_list(display_list, resource_storage, output.value()))- bitmap = output.release_value();+ auto frame = Gfx::DecodedImageFrame::create(*bitmap);+ if (!output.is_error() && !frame.is_error()) {+ Compositing::DisplayListResourceStorage resource_storage;+ auto display_list = Painting::record_image_frame_display_list(frame.value(), Gfx::FloatRect { {}, scaled_size.to_type<float>() }, Gfx::ScalingMode::BilinearMipmap, resource_storage);+ if (m_page->compositor_host().rasterize_display_list(display_list, resource_storage, output.value()))+ bitmap = output.release_value();+ }}}}diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cppindex b2a6406b40a..d64ac6b6ae2 100644--- a/Libraries/LibWeb/Page/EventHandler.cpp+++ b/Libraries/LibWeb/Page/EventHandler.cpp@@ -4101,9 +4101,12 @@ void EventHandler::maybe_show_context_menu(GC::Ref<DOM::Node> node, MouseEventCoimage_url = image_element->document().encoding_parse_url(image_element->current_src());if (image_url.has_value()) {- Optional<Gfx::Bitmap const*> bitmap;+ RefPtr<Gfx::Bitmap const> bitmap_of_current_frame;if (auto frame = image_element->current_image_frame(); frame.has_value())- bitmap = &frame->bitmap();+ bitmap_of_current_frame = frame->bitmap_sharing_pixels();+ Optional<Gfx::Bitmap const*> bitmap;+ if (bitmap_of_current_frame)+ bitmap = bitmap_of_current_frame.ptr();// NB: An image inside a link, or under an image map's link, offers both the image and the link.Optional<HTML::PreparedNavigationDescriptor> link_navigation;diff --git a/Libraries/LibWeb/SVG/SVGDecodedImageData.cpp b/Libraries/LibWeb/SVG/SVGDecodedImageData.cppindex c4e07f3f887..71a670c7f65 100644--- a/Libraries/LibWeb/SVG/SVGDecodedImageData.cpp+++ b/Libraries/LibWeb/SVG/SVGDecodedImageData.cpp@@ -222,7 +222,7 @@ size_t SVGDecodedImageData::external_memory_size() constsize_t size = Base::external_memory_size();size = JS::saturating_add_external_memory_size(size, JS::hash_map_external_memory_size(m_cached_rendered_frames));for (auto const& cached_frame : m_cached_rendered_frames)- size = JS::saturating_add_external_memory_size(size, cached_frame.value.bitmap().data_size());+ size = JS::saturating_add_external_memory_size(size, cached_frame.value.size_in_bytes());return size;}@@ -370,14 +370,17 @@ Optional<Gfx::DecodedImageFrame> SVGDecodedImageData::current_frame(Gfx::IntSizeif (!bitmap)return {};+ auto decoded_frame = Gfx::DecodedImageFrame::create(*bitmap);+ if (decoded_frame.is_error())+ return {};+// Prevent the cache from growing too big.// FIXME: Evict least used entries.if (m_cached_rendered_frames.size() > 10)m_cached_rendered_frames.remove(m_cached_rendered_frames.begin());- auto decoded_frame = Gfx::DecodedImageFrame { *bitmap };- m_cached_rendered_frames.set(size, decoded_frame);- return decoded_frame;+ m_cached_rendered_frames.set(size, decoded_frame.value());+ return decoded_frame.release_value();}Optional<Gfx::DecodedImageFrame> SVGDecodedImageData::default_frame(Gfx::IntSize size) constdiff --git a/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp b/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cppindex 933169585a6..6c4fba52e0a 100644--- a/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp+++ b/Libraries/LibWeb/WebGL/WebGLRenderingContextBase.cpp@@ -413,12 +413,16 @@ TexImageSourceResult web_webgl_read_tex_image_source(void* source_wrapper, TexImframe = image_frame.release_value();break;case TexImageSourceKind::HTMLCanvasElement:- if (auto bitmap = Bindings::impl_from<HTML::HTMLCanvasElement>(wrapper).get_bitmap_from_surface())- frame = Gfx::DecodedImageFrame { *bitmap };+ if (auto bitmap = Bindings::impl_from<HTML::HTMLCanvasElement>(wrapper).get_bitmap_from_surface()) {+ if (auto canvas_frame = Gfx::DecodedImageFrame::create(*bitmap); !canvas_frame.is_error())+ frame = canvas_frame.release_value();+ }break;case TexImageSourceKind::OffscreenCanvas:- if (auto bitmap = Bindings::impl_from<HTML::OffscreenCanvas>(wrapper).get_bitmap_from_surface())- frame = Gfx::DecodedImageFrame { *bitmap };+ if (auto bitmap = Bindings::impl_from<HTML::OffscreenCanvas>(wrapper).get_bitmap_from_surface()) {+ if (auto canvas_frame = Gfx::DecodedImageFrame::create(*bitmap); !canvas_frame.is_error())+ frame = canvas_frame.release_value();+ }break;case TexImageSourceKind::HTMLVideoElement:if (auto captured_frame = Bindings::impl_from<HTML::HTMLVideoElement>(wrapper).capture_current_video_frame(); captured_frame.has_value())@@ -442,15 +446,17 @@ TexImageSourceResult web_webgl_read_tex_image_pixels(u8 const* pixels, u32 widthif (!texture_export_format(format, type).has_value())return TexImageSourceResult::NoFrame;- // NB: The bitmap is shareable, so that the recorder sends it to the host without copying the pixels again.+ // NB: The bitmap is shareable, so that the frame shares its memory instead of copying the pixels again.auto bitmap = Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::RGBA8888, Gfx::AlphaType::Unpremultiplied, { static_cast<int>(width), static_cast<int>(height) });if (bitmap.is_error())return TexImageSourceResult::InvalidValue;auto row_size = static_cast<size_t>(width) * sizeof(u32);for (u32 y = 0; y < height; ++y)memcpy(bitmap.value()->scanline_u8(static_cast<int>(y)), pixels + y * row_size, row_size);- NonnullRefPtr<Gfx::Bitmap const> pixels_bitmap = bitmap.release_value();- return tex_image_source_frame(Compositing::WebGL::ImageSourceFrame { Gfx::DecodedImageFrame { move(pixels_bitmap) } }, format, type, out_frame);+ auto pixels_frame = Gfx::DecodedImageFrame::create(*bitmap.value());+ if (pixels_frame.is_error())+ return TexImageSourceResult::InvalidValue;+ return tex_image_source_frame(Compositing::WebGL::ImageSourceFrame { pixels_frame.release_value() }, format, type, out_frame);}}diff --git a/Services/Compositor/AnimatedImage.cpp b/Services/Compositor/AnimatedImage.cppindex 73d2defae29..2c3f985ff48 100644--- a/Services/Compositor/AnimatedImage.cpp+++ b/Services/Compositor/AnimatedImage.cpp@@ -175,9 +175,15 @@ bool AnimatedImage::receive_frames(Vector<NonnullRefPtr<Gfx::Bitmap>> bitmaps)if (find_slot(frame_index))continue;+ auto frame = Gfx::DecodedImageFrame::create(*bitmaps[i], m_color_space);+ if (frame.is_error()) {+ dbgln("Compositor: Dropping an animated image frame: {}", frame.error());+ continue;+ }+auto& slot = evict_oldest_slot();slot.frame_index = frame_index;- slot.frame = Gfx::DecodedImageFrame { *bitmaps[i], m_color_space };+ slot.frame = frame.release_value();slot.generation = ++m_write_generation;received_new_frame = true;}diff --git a/Services/Compositor/Canvas2DCommandPlayer.cpp b/Services/Compositor/Canvas2DCommandPlayer.cppindex a3aaec1581f..fc887a0726d 100644--- a/Services/Compositor/Canvas2DCommandPlayer.cpp+++ b/Services/Compositor/Canvas2DCommandPlayer.cpp@@ -178,7 +178,7 @@ Optional<SkPaint> Canvas2DCommandPlayer::paint_from(Canvas2DPaint const& style,auto const* frame = m_resolvers.image_frame(style.pattern_image_id.value());if (!frame)return {};- auto image = Gfx::sk_image_from_bitmap(frame->bitmap(), frame->color_space());+ auto image = Gfx::sk_image_from_decoded_image_frame(*frame);if (!image)break;@@ -461,7 +461,7 @@ void Canvas2DCommandPlayer::play_command(Canvas2DDrawImageFrame const& command)auto const* frame = m_resolvers.image_frame(command.frame_id.value());if (!frame)return;- auto image = Gfx::sk_image_from_bitmap(frame->bitmap(), frame->color_space());+ auto image = Gfx::sk_image_from_decoded_image_frame(*frame);if (!image)return;draw_image(*image, command.dst_rect, command.src_rect, command.scaling_mode, command.compositing);@@ -571,7 +571,7 @@ void Canvas2DCommandPlayer::play_command(Canvas2DPutImageFrame const& command)auto const* frame = m_resolvers.image_frame(command.frame_id.value());if (!frame)return;- m_surface->write_from_bitmap(frame->bitmap(), command.position, frame->color_space());+ m_surface->write_from_bitmap(*frame->bitmap_sharing_pixels(), command.position, frame->color_space());}}diff --git a/Services/Compositor/CompositorState.cpp b/Services/Compositor/CompositorState.cppindex 72f518dfe47..93164be6243 100644--- a/Services/Compositor/CompositorState.cpp+++ b/Services/Compositor/CompositorState.cpp@@ -617,7 +617,7 @@ void CompositorState::show_animated_image_frames_in(PresentedContext& context)for (auto& image_entry : images->value) {Compositing::ImageFrameResourceId id { image_entry.key };auto const& frame = image_entry.value->shown_frame();- if (frame.has_value() && context.presenter.resource_storage().has_image_frame(id) && &context.presenter.resource_storage().image_frame(id).bitmap() != &frame->bitmap())+ if (frame.has_value() && context.presenter.resource_storage().has_image_frame(id) && !context.presenter.resource_storage().image_frame(id).has_same_pixels_as(*frame))context.presenter.resource_storage().replace_image_frame_pixels(id, *frame);}// An image that nothing drew has been waiting for a context to draw it.diff --git a/Services/Compositor/DisplayListRasterCache.cpp b/Services/Compositor/DisplayListRasterCache.cppindex 3ef2ce1dd91..0d28a73ea4e 100644--- a/Services/Compositor/DisplayListRasterCache.cpp+++ b/Services/Compositor/DisplayListRasterCache.cpp@@ -58,15 +58,15 @@ struct DisplayListRasterCache::LayerImageFilterCache {struct DisplayListRasterCache::ImageFrameImage {AK_ALLOC_WITH_KMALLOC;- ImageFrameImage(NonnullRefPtr<Gfx::Bitmap const> bitmap, RefPtr<Gfx::SkiaBackendContext> skia_backend_context, sk_sp<SkImage> image)- : bitmap(move(bitmap))+ ImageFrameImage(Gfx::DecodedImageFrame frame, RefPtr<Gfx::SkiaBackendContext> skia_backend_context, sk_sp<SkImage> image)+ : frame(move(frame)), skia_backend_context(move(skia_backend_context)), image(move(image)){}- // A raster image reads the pixels of the bitmap, so the bitmap must live as long as the image.- NonnullRefPtr<Gfx::Bitmap const> bitmap;+ // A raster image reads the pixels of the frame, so the frame must live as long as the image.+ Gfx::DecodedImageFrame frame;RefPtr<Gfx::SkiaBackendContext> skia_backend_context;sk_sp<SkImage> image;};@@ -224,7 +224,7 @@ sk_sp<SkImageFilter> DisplayListRasterCache::backdrop_image_filter(u64 tree_strustatic sk_sp<SkImage> create_skia_image(Gfx::DecodedImageFrame const& frame, RefPtr<Gfx::SkiaBackendContext> const& skia_backend_context){- auto raster_image = Gfx::sk_image_from_bitmap(frame.bitmap(), frame.color_space());+ auto raster_image = Gfx::sk_image_from_decoded_image_frame(frame);auto* gr_context = skia_backend_context ? skia_backend_context->sk_context() : nullptr;if (!gr_context)return raster_image;@@ -240,14 +240,14 @@ sk_sp<SkImage> DisplayListRasterCache::image_for_image_frame(DisplayListResourceauto const& frame = resource_storage.image_frame(id);if (auto cached = m_image_frame_images.get(id.value()); cached.has_value()) {auto const& image = *cached.value();- // A frame replaced under the same id has a bitmap of its own, so this also catches a replacement that was+ // A frame replaced under the same id has pixels of its own, so this also catches a replacement that was// not evicted.- if (image.bitmap.ptr() == &frame.bitmap() && image.skia_backend_context.ptr() == skia_backend_context.ptr())+ if (image.frame.has_same_pixels_as(frame) && image.skia_backend_context.ptr() == skia_backend_context.ptr())return image.image;}auto image = create_skia_image(frame, skia_backend_context);- m_image_frame_images.set(id.value(), make<ImageFrameImage>(frame.bitmap_ref(), skia_backend_context, image));+ m_image_frame_images.set(id.value(), make<ImageFrameImage>(frame, skia_backend_context, image));return image;}diff --git a/Services/Compositor/HostWebGLContext.cpp b/Services/Compositor/HostWebGLContext.cppindex da2706290c6..116957eee35 100644--- a/Services/Compositor/HostWebGLContext.cpp+++ b/Services/Compositor/HostWebGLContext.cpp@@ -92,7 +92,7 @@ static ErrorOr<BitmapInColorSpace> bitmap_for_upload(HostWebGLContext::UploadSou{return source.visit([](Gfx::DecodedImageFrame const& frame) -> ErrorOr<BitmapInColorSpace> {- return BitmapInColorSpace { frame.bitmap_ref(), frame.color_space() };+ return BitmapInColorSpace { frame.bitmap_sharing_pixels(), frame.color_space() };},[&](NonnullRefPtr<Media::VideoFrame> const& video_frame) -> ErrorOr<BitmapInColorSpace> {return bitmap_from_video_frame(*video_frame, skia_backend_context);diff --git a/Services/Compositor/SkiaUtils.cpp b/Services/Compositor/SkiaUtils.cppindex d6a01c11c17..8ef188dd8cc 100644--- a/Services/Compositor/SkiaUtils.cpp+++ b/Services/Compositor/SkiaUtils.cpp@@ -378,7 +378,7 @@ sk_sp<SkImageFilter> SkiaFilterReader::filter()auto src_rect = rect();auto dest_rect = rect();auto sampling_options = to_skia_sampling_options(value<ScalingMode>());- auto image = sk_image_from_bitmap(frame.bitmap(), frame.color_space());+ auto image = sk_image_from_decoded_image_frame(frame);return SkImageFilters::Image(move(image), src_rect, dest_rect, sampling_options);}case FilterOperationType::Merge: {@@ -454,15 +454,25 @@ sk_sp<SkImageFilter> to_skia_image_filter(ReadonlyBytes serialized_filter, Functreturn SkiaFilterReader { serialized_filter, image_frame, blur_edge }.read_graph();}-sk_sp<SkImage> sk_image_from_bitmap(Bitmap const& bitmap, ColorSpace const& color_space)+static sk_sp<SkImage> sk_image_borrowing_pixels(IntSize size, BitmapFormat format, AlphaType alpha_type, ColorSpace const& color_space, void const* pixels, size_t pitch){- auto info = SkImageInfo::Make(bitmap.width(), bitmap.height(), to_skia_color_type(bitmap.format()), to_skia_alpha_type(bitmap.format(), bitmap.alpha_type()), to_skia_color_space(color_space));+ auto info = SkImageInfo::Make(size.width(), size.height(), to_skia_color_type(format), to_skia_alpha_type(format, alpha_type), to_skia_color_space(color_space));SkBitmap sk_bitmap;- sk_bitmap.installPixels(info, const_cast<void*>(static_cast<void const*>(bitmap.scanline(0))), bitmap.pitch());+ sk_bitmap.installPixels(info, const_cast<void*>(pixels), pitch);sk_bitmap.setImmutable();return sk_bitmap.asImage();}+sk_sp<SkImage> sk_image_from_bitmap(Bitmap const& bitmap, ColorSpace const& color_space)+{+ return sk_image_borrowing_pixels(bitmap.size(), bitmap.format(), bitmap.alpha_type(), color_space, bitmap.scanline(0), bitmap.pitch());+}++sk_sp<SkImage> sk_image_from_decoded_image_frame(DecodedImageFrame const& frame)+{+ return sk_image_borrowing_pixels(frame.size(), frame.format(), frame.alpha_type(), frame.color_space(), frame.pixels().data(), frame.pitch());+}+sk_sp<SkImage> sk_image_adopting_bitmap(NonnullRefPtr<Bitmap> bitmap, ColorSpace const& color_space){auto info = SkImageInfo::Make(bitmap->width(), bitmap->height(), to_skia_color_type(bitmap->format()), to_skia_alpha_type(bitmap->format(), bitmap->alpha_type()), to_skia_color_space(color_space));diff --git a/Services/Compositor/SkiaUtils.h b/Services/Compositor/SkiaUtils.hindex 4aec34296d3..d94f006ed33 100644--- a/Services/Compositor/SkiaUtils.h+++ b/Services/Compositor/SkiaUtils.h@@ -157,6 +157,7 @@ sk_sp<SkBlender> to_skia_blender(Gfx::CompositingAndBlendingOperator compositingsk_sp<SkColorSpace> to_skia_color_space(ColorSpace const&);sk_sp<SkImage> sk_image_from_bitmap(Bitmap const& bitmap, ColorSpace const& color_space);+sk_sp<SkImage> sk_image_from_decoded_image_frame(DecodedImageFrame const&);// Hands the source bitmap's pixel storage to the returned SkImage without copying: the image// holds a reference to the bitmap and drops it on destruction.diff --git a/Tests/Compositor/TestDisplayListImageFilters.cpp b/Tests/Compositor/TestDisplayListImageFilters.cppindex 84952932dd1..a1395e8c55d 100644--- a/Tests/Compositor/TestDisplayListImageFilters.cpp+++ b/Tests/Compositor/TestDisplayListImageFilters.cpp@@ -21,7 +21,7 @@ static NonnullRefPtr<Gfx::Bitmap> replace_image(Compositing::DisplayListResourceauto bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, rect.size()));bitmap->set_pixel(0, 0, color);Compositing::DisplayListResourceTransaction transaction;- transaction.image_frames.append({ image_id, Gfx::DecodedImageFrame { *bitmap } });+ transaction.image_frames.append({ image_id, MUST(Gfx::DecodedImageFrame::create(*bitmap)) });cache.evict(storage.apply_transaction(move(transaction)));return bitmap;}diff --git a/Tests/LibWeb/TestStructuredSerializeFormat.cpp b/Tests/LibWeb/TestStructuredSerializeFormat.cppindex 72bea1b85b5..984a684bdc2 100644--- a/Tests/LibWeb/TestStructuredSerializeFormat.cpp+++ b/Tests/LibWeb/TestStructuredSerializeFormat.cpp@@ -467,7 +467,7 @@ TEST_CASE(image_serializables_encode_to_frozen_bytes){auto bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, { 2, 2 }));auto color_space = MUST(Gfx::ColorSpace::from_cicp({ Media::ColorPrimaries::SMPTE432, Media::TransferCharacteristics::SRGB, Media::MatrixCoefficients::Identity, Media::VideoFullRangeFlag::Full }));- auto image_bitmap = Web::HTML::ImageBitmap::create(Gfx::DecodedImageFrame { *bitmap, move(color_space) });+ auto image_bitmap = Web::HTML::ImageBitmap::create(MUST(Gfx::DecodedImageFrame::create(*bitmap, move(color_space))));ReadonlyBytes data { bitmap->scanline_u8(0), bitmap->data_size() };Array<u8, 3> display_p3 { 1, 12, 13 }; // CICP, SMPTE432 primaries, sRGB transfer characteristics@@ -542,21 +542,19 @@ TEST_CASE(image_bitmap_transfer_keeps_pixels_out_of_the_message_payload)auto result = transfer_through_ipc(realm, wrapper, transfer);EXPECT(source->is_detached());- EXPECT_EQ(source->bitmap(), nullptr);+ EXPECT_EQ(source->frame(), nullptr);EXPECT(result.payload_size < 4096);EXPECT_EQ(result.attachment_count, 1u);auto& received = unwrap_wrappable<Web::HTML::ImageBitmap>(result.transferred_values[0]);- // A bitmap that has the shared memory to itself is backed by it, so that passing it on does not copy it.- EXPECT(received.bitmap()->anonymous_buffer().is_valid());EXPECT_EQ(received.width(), 1024u);EXPECT_EQ(received.height(), 768u);- EXPECT_EQ(received.bitmap()->format(), Gfx::BitmapFormat::RGBA8888);- EXPECT_EQ(received.bitmap()->alpha_type(), Gfx::AlphaType::Unpremultiplied);- EXPECT_EQ(received.bitmap()->get_pixel(0, 0), Gfx::Color::Red);- EXPECT_EQ(received.bitmap()->get_pixel(1023, 767), Gfx::Color::Blue);+ EXPECT_EQ(received.frame()->format(), Gfx::BitmapFormat::RGBA8888);+ EXPECT_EQ(received.frame()->alpha_type(), Gfx::AlphaType::Unpremultiplied);+ EXPECT_EQ(received.frame()->bitmap_sharing_pixels()->get_pixel(0, 0), Gfx::Color::Red);+ EXPECT_EQ(received.frame()->bitmap_sharing_pixels()->get_pixel(1023, 767), Gfx::Color::Blue);received.close();- EXPECT_EQ(received.bitmap(), nullptr);+ EXPECT_EQ(received.frame(), nullptr);}TEST_CASE(any_number_of_transferred_image_bitmaps_share_one_attachment)@@ -565,10 +563,11 @@ TEST_CASE(any_number_of_transferred_image_bitmaps_share_one_attachment)static constexpr size_t count = IPC::MAX_MESSAGE_FD_COUNT + 1;GC::RootVector<GC::Ref<JS::Object>> transfer;GC::RootVector<JS::Value> values;+ auto shared_memory_of_every_source_frame = MUST(Core::AnonymousBuffer::create_with_size(count * sizeof(Gfx::RawPixel)));for (size_t i = 0; i < count; ++i) {- auto bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, { 1, 1 }));- bitmap->set_pixel(0, 0, Gfx::Color(i % 256, i / 256, 0));- auto wrapper = wrap_image_bitmap(realm, create_image_bitmap(bitmap));+ shared_memory_of_every_source_frame.data<Gfx::RawPixel>()[i] = Gfx::Color(i % 256, i / 256, 0).value();+ auto frame = MUST(Gfx::DecodedImageFrame::create_over_shared_memory(shared_memory_of_every_source_frame, i * sizeof(Gfx::RawPixel), { 1, 1 }, Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied));+ auto wrapper = wrap_image_bitmap(realm, Web::HTML::ImageBitmap::create(move(frame)));transfer.append(wrapper);values.append(wrapper);}@@ -592,8 +591,7 @@ TEST_CASE(any_number_of_transferred_image_bitmaps_share_one_attachment)EXPECT_EQ(result.transferred_values.size(), count + 2);for (size_t i = 0; i < count; ++i) {auto& received = unwrap_wrappable<Web::HTML::ImageBitmap>(result.transferred_values[i]);- EXPECT(!received.bitmap()->anonymous_buffer().is_valid());- EXPECT_EQ(received.bitmap()->get_pixel(0, 0), Gfx::Color(i % 256, i / 256, 0));+ EXPECT_EQ(received.frame()->bitmap_sharing_pixels()->get_pixel(0, 0), Gfx::Color(i % 256, i / 256, 0));received.close();}}@@ -608,10 +606,10 @@ TEST_CASE(image_bitmap_with_padded_rows_transfers_its_pixels)auto result = transfer_through_ipc(realm, wrapper, transfer);auto& received = unwrap_wrappable<Web::HTML::ImageBitmap>(result.transferred_values[0]);- EXPECT_EQ(received.bitmap()->get_pixel(0, 0), Gfx::Color::Red);- EXPECT_EQ(received.bitmap()->get_pixel(1, 0), Gfx::Color::Green);- EXPECT_EQ(received.bitmap()->get_pixel(0, 1), Gfx::Color::Blue);- EXPECT_EQ(received.bitmap()->get_pixel(1, 1), Gfx::Color::White);+ EXPECT_EQ(received.frame()->bitmap_sharing_pixels()->get_pixel(0, 0), Gfx::Color::Red);+ EXPECT_EQ(received.frame()->bitmap_sharing_pixels()->get_pixel(1, 0), Gfx::Color::Green);+ EXPECT_EQ(received.frame()->bitmap_sharing_pixels()->get_pixel(0, 1), Gfx::Color::Blue);+ EXPECT_EQ(received.frame()->bitmap_sharing_pixels()->get_pixel(1, 1), Gfx::Color::White);received.close();}@@ -640,7 +638,7 @@ TEST_CASE(image_bitmaps_queued_on_a_transferred_message_port_keep_their_pixels)auto& realm = test_realm();auto port = Web::HTML::MessagePort::create(realm);static constexpr size_t count = IPC::MAX_MESSAGE_FD_COUNT + 1;- auto bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, { 1, 1 }));+ auto bitmap = MUST(Gfx::Bitmap::create_shareable(Gfx::BitmapFormat::BGRA8888, Gfx::AlphaType::Premultiplied, { 1, 1 }));bitmap->set_pixel(0, 0, Gfx::Color::Blue);GC::RootVector<GC::Ref<JS::Object>> transfer;GC::RootVector<JS::Value> values;@@ -667,7 +665,7 @@ TEST_CASE(image_bitmaps_queued_on_a_transferred_message_port_keep_their_pixels)EXPECT_EQ(received_values.size(), count);for (auto value : received_values) {auto& received = unwrap_wrappable<Web::HTML::ImageBitmap>(value);- EXPECT_EQ(received.bitmap()->get_pixel(0, 0), Gfx::Color::Blue);+ EXPECT_EQ(received.frame()->bitmap_sharing_pixels()->get_pixel(0, 0), Gfx::Color::Blue);received.close();}}@@ -712,7 +710,7 @@ TEST_CASE(image_bitmap_transfer_inlines_pixels_without_shared_memory)EXPECT(transfer_record.transfer_data_holders[0].buffer().attachments().is_empty());auto received_values = deserialize_transferred_values(realm, transfer_record);auto& received = unwrap_wrappable<Web::HTML::ImageBitmap>(received_values[0]);- EXPECT_EQ(received.bitmap()->get_pixel(0, 0), Gfx::Color::Green);+ EXPECT_EQ(received.frame()->bitmap_sharing_pixels()->get_pixel(0, 0), Gfx::Color::Green);received.close();}#endif@@ -781,7 +779,7 @@ TEST_CASE(image_bitmap_transfer_protects_against_backing_truncation)# elseEXPECT_EQ(ftruncate(buffer.fd(), 0), 0);# endif- EXPECT_EQ(receiver.bitmap()->get_pixel(0, 0), Gfx::Color::Red);+ EXPECT_EQ(receiver.frame()->bitmap_sharing_pixels()->get_pixel(0, 0), Gfx::Color::Red);receiver.close();}#endifdiff --git a/UI/Android/src/main/cpp/WebViewImplementationNative.cpp b/UI/Android/src/main/cpp/WebViewImplementationNative.cppindex c31d2f50f5d..4dc604879a6 100644--- a/UI/Android/src/main/cpp/WebViewImplementationNative.cpp+++ b/UI/Android/src/main/cpp/WebViewImplementationNative.cpp@@ -77,7 +77,7 @@ void WebViewImplementationNative::paint_into_bitmap(void* android_bitmap_raw, Anauto android_bitmap = MUST(Gfx::Bitmap::create_wrapper(to_gfx_bitmap_format(info.format), Gfx::AlphaType::Premultiplied, { info.width, info.height }, info.stride, android_bitmap_raw));auto painter = Gfx::Painter::create(android_bitmap);if (auto* bitmap = m_client_state.has_usable_bitmap ? m_client_state.front_bitmap.bitmap.ptr() : m_backup_bitmap.ptr())- painter->draw_bitmap(android_bitmap->rect().to_type<float>(), Gfx::DecodedImageFrame { *MUST(bitmap->clone()) }, bitmap->rect(), Gfx::ScalingMode::NearestNeighbor, {}, 1.0f, Gfx::CompositingAndBlendingOperator::Copy);+ painter->draw_bitmap(android_bitmap->rect().to_type<float>(), MUST(Gfx::DecodedImageFrame::create(*MUST(bitmap->clone()))), bitmap->rect(), Gfx::ScalingMode::NearestNeighbor, {}, 1.0f, Gfx::CompositingAndBlendingOperator::Copy);elsepainter->fill_rect(android_bitmap->rect().to_type<float>(), Gfx::Color::Magenta);}