Skip to content

Truly independent web browser

LibWeb+LibCompositing+Compositor: Make putImageData() ignore the clip

Andreas Kling0b89ac623e73
diff --git a/Libraries/LibCompositing/Rust/src/display_list/canvas_2d.rs b/Libraries/LibCompositing/Rust/src/display_list/canvas_2d.rs
index a0f366a500d..b224a9ef4ef 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/canvas_2d.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/canvas_2d.rs
@@ -12,8 +12,8 @@ use super::commands::*;
use super::ffi_bytes::FfiBytes;
use super::validate::validate_canvas_2d_tape;
use libgfx_rust::{
- AffineTransform, CapStyle, Color, CompositingAndBlendingOperator, FloatPoint, FloatRect, IntRect, JoinStyle,
- ScalingMode, WindingRule,
+ AffineTransform, CapStyle, Color, CompositingAndBlendingOperator, FloatPoint, FloatRect, IntPoint, IntRect,
+ JoinStyle, ScalingMode, WindingRule,
};
use std::ffi::c_void;
@@ -377,6 +377,10 @@ impl TapeWriter {
pub fn clear_canvas(&mut self, color: Color) {
self.append_plain(&Canvas2DClearCanvas { color });
}
+
+ pub fn put_image_frame(&mut self, frame_id: ImageFrameResourceId, position: IntPoint) {
+ self.append_plain(&Canvas2DPutImageFrame { frame_id, position });
+ }
}
/// Checks the tape of a segment of the canvas 2D stream that came from another process. Returns false with a static
diff --git a/Libraries/LibCompositing/Rust/src/display_list/canvas_2d_recorder.rs b/Libraries/LibCompositing/Rust/src/display_list/canvas_2d_recorder.rs
index 831fad43e87..4b7d1cb60f6 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/canvas_2d_recorder.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/canvas_2d_recorder.rs
@@ -9,7 +9,9 @@
use super::canvas_2d::{Compositing, Paint, Stroke, TapeWriter};
use super::commands::*;
-use libgfx_rust::{AffineTransform, Color, FloatPoint, FloatRect, IntRect, IntSize, ScalingMode, WindingRule};
+use libgfx_rust::{
+ AffineTransform, Color, FloatPoint, FloatRect, IntPoint, IntRect, IntSize, ScalingMode, WindingRule,
+};
/// The number of commands that the shared stream carries to the Compositor at most in one batch. A batch holds the
/// image frames of its commands, each a bitmap that an IPC attachment carries, so it has to stay small.
@@ -181,6 +183,12 @@ impl<T: Transport> Recorder<T> {
self.record(|tape| tape.draw_image_frame(frame_id, dst_rect, src_rect, scaling_mode, compositing));
}
+ /// Sets the pixels of the canvas at the position to those of the frame, as putImageData() does.
+ pub fn put_image_frame(&mut self, frame: &T::ImageFrame, position: IntPoint) {
+ let frame_id = self.transport.add_image_frame(frame);
+ self.record(|tape| tape.put_image_frame(frame_id, position));
+ }
+
pub fn draw_canvas(
&mut self,
source_canvas_id: CanvasId,
diff --git a/Libraries/LibCompositing/Rust/src/display_list/commands.rs b/Libraries/LibCompositing/Rust/src/display_list/commands.rs
index a606204503f..17f88988920 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/commands.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/commands.rs
@@ -68,10 +68,11 @@ pub enum DisplayListCommandType {
Canvas2DClipPath,
Canvas2DReset,
Canvas2DClearCanvas,
+ Canvas2DPutImageFrame,
}
ffi_enum_bytes!(DisplayListCommandType as u8);
-pub const DISPLAY_LIST_COMMAND_TYPE_COUNT: usize = DisplayListCommandType::Canvas2DClearCanvas as usize + 1;
+pub const DISPLAY_LIST_COMMAND_TYPE_COUNT: usize = DisplayListCommandType::Canvas2DPutImageFrame as usize + 1;
impl DisplayListCommandType {
pub fn from_u8(value: u8) -> Option<Self> {
@@ -119,6 +120,7 @@ impl DisplayListCommandType {
| Self::Canvas2DClipPath
| Self::Canvas2DReset
| Self::Canvas2DClearCanvas
+ | Self::Canvas2DPutImageFrame
)
}
@@ -176,6 +178,7 @@ impl DisplayListCommandType {
Self::Canvas2DClipPath => "Canvas2DClipPath",
Self::Canvas2DReset => "Canvas2DReset",
Self::Canvas2DClearCanvas => "Canvas2DClearCanvas",
+ Self::Canvas2DPutImageFrame => "Canvas2DPutImageFrame",
}
}
}
@@ -2058,6 +2061,20 @@ impl DisplayListCommand for Canvas2DClearCanvas {
const COMMAND_TYPE: DisplayListCommandType = DisplayListCommandType::Canvas2DClearCanvas;
}
+// Sets the pixels of the canvas at the position to those of the frame, regardless of the transform, the clip and the
+// compositing of the context, as putImageData() does.
+#[derive(Clone, Copy, Debug, PartialEq)]
+#[repr(C)]
+pub struct Canvas2DPutImageFrame {
+ pub frame_id: ImageFrameResourceId,
+ pub position: IntPoint,
+}
+ffi_bytes_fields!(Canvas2DPutImageFrame { frame_id, position });
+
+impl DisplayListCommand for Canvas2DPutImageFrame {
+ const COMMAND_TYPE: DisplayListCommandType = DisplayListCommandType::Canvas2DPutImageFrame;
+}
+
pub fn int_rect_from_two_points(a: IntPoint, b: IntPoint) -> IntRect {
IntRect::new(a.x.min(b.x), a.y.min(b.y), (a.x - b.x).abs(), (a.y - b.y).abs())
}
diff --git a/Libraries/LibCompositing/Rust/src/display_list/summary.rs b/Libraries/LibCompositing/Rust/src/display_list/summary.rs
index 90a857b2bd8..3fac37e244a 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/summary.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/summary.rs
@@ -151,7 +151,8 @@ fn note_referenced_ids(command_type: DisplayListCommandType, payload: &[u8], ids
| T::Canvas2DRestore
| T::Canvas2DClipPath
| T::Canvas2DReset
- | T::Canvas2DClearCanvas => {}
+ | T::Canvas2DClearCanvas
+ | T::Canvas2DPutImageFrame => {}
}
}
@@ -304,7 +305,8 @@ fn command_blend(
| T::Canvas2DRestore
| T::Canvas2DClipPath
| T::Canvas2DReset
- | T::Canvas2DClearCanvas => None,
+ | T::Canvas2DClearCanvas
+ | T::Canvas2DPutImageFrame => None,
}
}
diff --git a/Libraries/LibCompositing/Rust/src/display_list/validate.rs b/Libraries/LibCompositing/Rust/src/display_list/validate.rs
index 0a88a4eccfc..cd61355ea26 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/validate.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/validate.rs
@@ -345,6 +345,7 @@ fn command_struct_size(command_type: DisplayListCommandType) -> usize {
T::Canvas2DClipPath => size_of::<Canvas2DClipPath>(),
T::Canvas2DReset => size_of::<Canvas2DReset>(),
T::Canvas2DClearCanvas => size_of::<Canvas2DClearCanvas>(),
+ T::Canvas2DPutImageFrame => size_of::<Canvas2DPutImageFrame>(),
}
}
@@ -584,7 +585,8 @@ fn validate_command_fields(
| T::Canvas2DSave
| T::Canvas2DRestore
| T::Canvas2DReset
- | T::Canvas2DClearCanvas => {}
+ | T::Canvas2DClearCanvas
+ | T::Canvas2DPutImageFrame => {}
}
Ok(())
}
diff --git a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_image_data.rs b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_image_data.rs
index 29fb179cb43..94d8734923c 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/html/canvas_image_data.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/html/canvas_image_data.rs
@@ -13,9 +13,8 @@
use core::ptr::NonNull;
use ak::Utf16String;
-use libcompositing_rust::display_list::canvas_2d::Compositing;
use libcompositing_rust::display_list::canvas_2d_recorder::AlphaType;
-use libgfx_rust::{AffineTransform, CompositingAndBlendingOperator, FloatRect, IntRect, ScalingMode};
+use libgfx_rust::{IntPoint, IntRect};
use libjs_rust::api::{ErrorKind, ErrorType};
use super::image_data::{ImageData, ImageDataSettings};
@@ -181,40 +180,15 @@ impl ContextState {
.into());
};
- #[allow(
- clippy::cast_precision_loss,
- reason = "the rectangle is in floats, as the C++ had it"
- )]
- let destination = FloatRect::new(
- destination_rect.x as f32,
- destination_rect.y as f32,
- destination_rect.width as f32,
- destination_rect.height as f32,
- );
- let frame_rect = IntRect {
- x: 0,
- y: 0,
- width: destination_rect.width,
- height: destination_rect.height,
- };
let lent_frame = LentImageFrame(NonNull::new(frame.as_raw().cast_mut()).expect("an image frame has a frame"));
self.record(|recorder| {
- recorder.save();
- recorder.set_transform(AffineTransform::identity());
- // NB: Step 7 sets each destination pixel to the source pixel: putImageData replaces the pixels (including
- // their alpha), rather than compositing them. The copy operator makes a transparent source pixel
- // overwrite an opaque destination pixel, rather than leave it unchanged.
- recorder.draw_image_frame(
+ recorder.put_image_frame(
&lent_frame,
- destination,
- frame_rect,
- ScalingMode::NearestNeighbor,
- &Compositing {
- compositing_and_blending_operator: CompositingAndBlendingOperator::Copy,
- ..Compositing::default()
+ IntPoint {
+ x: destination_rect.x,
+ y: destination_rect.y,
},
);
- recorder.restore();
});
self.did_draw();
diff --git a/Libraries/LibWeb/Rust/src/painting/display_list/dump.rs b/Libraries/LibWeb/Rust/src/painting/display_list/dump.rs
index da529bc2a21..e3c88fe55f6 100644
--- a/Libraries/LibWeb/Rust/src/painting/display_list/dump.rs
+++ b/Libraries/LibWeb/Rust/src/painting/display_list/dump.rs
@@ -690,7 +690,8 @@ fn dump_command(output: &mut String, command_type: DisplayListCommandType, paylo
| DisplayListCommandType::Canvas2DRestore
| DisplayListCommandType::Canvas2DClipPath
| DisplayListCommandType::Canvas2DReset
- | DisplayListCommandType::Canvas2DClearCanvas => {}
+ | DisplayListCommandType::Canvas2DClearCanvas
+ | DisplayListCommandType::Canvas2DPutImageFrame => {}
}
}
diff --git a/Services/Compositor/Canvas2DCommandPlayer.cpp b/Services/Compositor/Canvas2DCommandPlayer.cpp
index 76618708171..d3933437218 100644
--- a/Services/Compositor/Canvas2DCommandPlayer.cpp
+++ b/Services/Compositor/Canvas2DCommandPlayer.cpp
@@ -290,4 +290,15 @@ void Canvas2DCommandPlayer::play_command(Canvas2DClearCanvas const& command)
m_surface->write_from_bitmap(*strip, { 0, y }, m_color_space);
}
+// https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-putimagedata
+void Canvas2DCommandPlayer::play_command(Canvas2DPutImageFrame const& command)
+{
+ // NB: putImageData() sets the pixels of the output bitmap, so they are written to the surface directly, without
+ // the current transformation matrix, the clipping region, or compositing.
+ 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());
+}
+
}
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/pixel-manipulation/2d.imageData.put.clip.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/pixel-manipulation/2d.imageData.put.clip.txt
index db696833e10..d04e444e0eb 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/pixel-manipulation/2d.imageData.put.clip.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/element/pixel-manipulation/2d.imageData.put.clip.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail putImageData() is not affected by clipping regions
\ No newline at end of file
+1 Pass
+Pass putImageData() is not affected by clipping regions
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/pixel-manipulation/2d.imageData.put.clip.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/pixel-manipulation/2d.imageData.put.clip.txt
index db696833e10..d04e444e0eb 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/pixel-manipulation/2d.imageData.put.clip.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/pixel-manipulation/2d.imageData.put.clip.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail putImageData() is not affected by clipping regions
\ No newline at end of file
+1 Pass
+Pass putImageData() is not affected by clipping regions
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/pixel-manipulation/2d.imageData.put.clip.worker.txt b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/pixel-manipulation/2d.imageData.put.clip.worker.txt
index db696833e10..d04e444e0eb 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/pixel-manipulation/2d.imageData.put.clip.worker.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/html/canvas/offscreen/pixel-manipulation/2d.imageData.put.clip.worker.txt
@@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
-1 Fail
-Fail putImageData() is not affected by clipping regions
\ No newline at end of file
+1 Pass
+Pass putImageData() is not affected by clipping regions
\ No newline at end of file