Truly independent web browser
LibGfx+LibWeb: Give image frames a row pitch of their own
Aliaksandr Kalenik4598762fc3cf
diff --git a/Libraries/LibGfx/DecodedImageFrame.cpp b/Libraries/LibGfx/DecodedImageFrame.cppindex 01b2cc10bc1..980ed7a425b 100644--- a/Libraries/LibGfx/DecodedImageFrame.cpp+++ b/Libraries/LibGfx/DecodedImageFrame.cpp@@ -31,6 +31,7 @@ extern "C" void* ladybird_gfx_decoded_image_frame_retain(void const* frame, Gfx:.bitmap_format_value = to_underlying(typed_frame.format()),.alpha_type_value = to_underlying(typed_frame.alpha_type()),.pixels = typed_frame.pixels().data(),+ .pitch = typed_frame.pitch(),};return new Gfx::DecodedImageFrame(typed_frame);}diff --git a/Libraries/LibGfx/DecodedImageFrame.h b/Libraries/LibGfx/DecodedImageFrame.hindex 889c11733be..d839f6fa94d 100644--- a/Libraries/LibGfx/DecodedImageFrame.h+++ b/Libraries/LibGfx/DecodedImageFrame.h@@ -56,8 +56,8 @@ public: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()); }+ size_t pitch() const { return m_pitch; }+ size_t size_in_bytes() const { return Bitmap::size_in_bytes(m_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; }@@ -73,6 +73,7 @@ private:: m_id(next_id()), m_shared_memory(move(shared_memory)), m_offset_of_pixels(offset_of_pixels)+ , m_pitch(Bitmap::minimum_pitch(size.width(), format)), m_size(size), m_format(format), m_alpha_type(alpha_type)@@ -89,6 +90,7 @@ private:u64 m_id { 0 };Core::AnonymousBuffer m_shared_memory;size_t m_offset_of_pixels { 0 };+ size_t m_pitch { 0 };IntSize m_size;BitmapFormat m_format { BitmapFormat::Invalid };AlphaType m_alpha_type { AlphaType::Premultiplied };diff --git a/Libraries/LibGfx/Rust/src/image_frame.rs b/Libraries/LibGfx/Rust/src/image_frame.rsindex 131a76c3e4b..a31940d0724 100644--- a/Libraries/LibGfx/Rust/src/image_frame.rs+++ b/Libraries/LibGfx/Rust/src/image_frame.rs@@ -25,6 +25,7 @@ pub struct FfiImageFrameSnapshot {pub bitmap_format_value: i32,pub alpha_type_value: i32,pub pixels: *const u8,+ pub pitch: usize,}impl Default for FfiImageFrameSnapshot {@@ -36,6 +37,7 @@ impl Default for FfiImageFrameSnapshot {bitmap_format_value: 0,alpha_type_value: 0,pixels: std::ptr::null(),+ pitch: 0,}}}@@ -124,8 +126,14 @@ impl ImageFrameHandle {&& self.alpha_type_value() == ALPHA_TYPE_VALUE_OF_UNPREMULTIPLIED}+ /// The bytes between the starts of two rows, which can be more than the rows hold.#[inline]pub fn pitch(&self) -> usize {+ self.0.snapshot.pitch+ }++ #[inline]+ pub fn minimum_pitch(&self) -> usize {usize::try_from(self.width()).expect("a frame has a width") * BYTES_PER_PIXEL}diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rs b/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rsindex 6d03eef7494..83ca79620bb 100644--- a/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/html/image_bitmap.rs@@ -9,6 +9,7 @@use core::cell::{Cell, RefCell};use core::ffi::c_void;+use std::borrow::Cow;use ak::Utf16String;use libgfx_rust::image_frame::ImageFrameHandle;@@ -163,10 +164,21 @@ fn frame_color_space(frame: &ImageFrameHandle) -> FrameColorSpace {color_space}-/// The pixels of the frame, which borrow from it.-fn frame_bytes(frame: &ImageFrameHandle) -> &[u8] {+/// The pixels of the frame in rows `minimum_pitch()` bytes apart, so that what is written does not depend on how far+/// apart the memory of this machine places them. They borrow from the frame unless its rows are farther apart.+fn tightly_packed_frame_bytes(frame: &ImageFrameHandle) -> Cow<'_, [u8]> {// SAFETY: The handle keeps the pixels mapped, and nothing writes to the pixels of a frame.- unsafe { ak::slice_from_raw_parts(frame.pixels().as_ptr(), frame.size_in_bytes()) }+ let bytes = unsafe { ak::slice_from_raw_parts(frame.pixels().as_ptr(), frame.size_in_bytes()) };+ let row_size = frame.minimum_pitch();+ if frame.pitch() == row_size {+ return Cow::Borrowed(bytes);+ }+ let rows = bytes.chunks_exact(frame.pitch());+ let mut tightly_packed_bytes = Vec::with_capacity(rows.len() * row_size);+ for row in rows {+ tightly_packed_bytes.extend_from_slice(&row[..row_size]);+ }+ Cow::Owned(tightly_packed_bytes)}/// Writes a copy of the bitmap data of the frame, or that there is none.@@ -185,10 +197,10 @@ fn serialize_bitmap_data(serialized: &mut SerializationWriter, frame: Option<&Im});serialized.write_i32(frame.width());serialized.write_i32(frame.height());- serialized.write_u64(frame.pitch() as u64);+ serialized.write_u64(frame.minimum_pitch() as u64);serialize_enumeration(serialized, &BITMAP_FORMATS, frame.bitmap_format_value(), for_storage);serialize_enumeration(serialized, &ALPHA_TYPES, frame.alpha_type_value(), for_storage);- serialized.write_bytes(frame_bytes(frame));+ serialized.write_bytes(&tightly_packed_frame_bytes(frame));if !has_color_space {return;}@@ -251,10 +263,10 @@ fn write_transferred_bitmap_data(data_holder: &mut TransferDataWriter, frame: Op});data_holder.write(&frame.width());data_holder.write(&frame.height());- data_holder.write(&(frame.pitch() as u64));+ data_holder.write(&(frame.minimum_pitch() as u64));data_holder.write(&frame.bitmap_format_value());data_holder.write(&frame.alpha_type_value());- data_holder.write(frame_bytes(frame));+ data_holder.write(&*tightly_packed_frame_bytes(frame));if has_color_space {write_transferred_color_space(data_holder, &color_space);}diff --git a/Libraries/LibWeb/HTML/ImageBitmap.cpp b/Libraries/LibWeb/HTML/ImageBitmap.cppindex b51c1202282..07c59ce4273 100644--- a/Libraries/LibWeb/HTML/ImageBitmap.cpp+++ b/Libraries/LibWeb/HTML/ImageBitmap.cpp@@ -99,7 +99,8 @@ struct SharedPixelsOfFrame {size_t ImageBitmap::shared_pixels_size(Gfx::DecodedImageFrame const& frame){- return round_up_to_power_of_two(frame.size_in_bytes(), shared_pixels_alignment);+ auto const tightly_packed_size = Gfx::Bitmap::size_in_bytes(Gfx::Bitmap::minimum_pitch(frame.width(), frame.format()), frame.height());+ return round_up_to_power_of_two(tightly_packed_size, shared_pixels_alignment);}// Hands a decoded frame over to the Rust implementation, which takes it over, or null for none.@@ -224,7 +225,12 @@ bool web_image_bitmap_copy_to_shared_pixels(ImageBitmapSharedPixels* shared_pixe{if (!shared_pixels || ImageBitmap::shared_pixels_size(*frame) > shared_pixels->buffer.size() - shared_pixels->used_size)return false;- frame->pixels().copy_to({ shared_pixels->buffer.data<u8>() + shared_pixels->used_size, frame->size_in_bytes() });+ // NB: The receiver reads the rows as tightly packed, which the rows of the frame need not be.+ auto const row_size = Gfx::Bitmap::minimum_pitch(frame->width(), frame->format());+ auto const pixels = frame->pixels();+ auto* destination = shared_pixels->buffer.data<u8>() + shared_pixels->used_size;+ for (int y = 0; y < frame->height(); ++y)+ __builtin_memcpy(destination + static_cast<size_t>(y) * row_size, pixels.offset(static_cast<size_t>(y) * frame->pitch()), row_size);*buffer_index = shared_pixels->buffer_index;*offset = shared_pixels->used_size;shared_pixels->used_size += ImageBitmap::shared_pixels_size(*frame);