Skip to content

Truly independent web browser

LibWeb: Stop re-recording a canvas when it draws

Aliaksandr Kalenik5c010dff5a4d
diff --git a/Libraries/LibCompositing/Rust/src/display_list/commands.rs b/Libraries/LibCompositing/Rust/src/display_list/commands.rs
index 494424f19e5..baccb0a2bd3 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/commands.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/commands.rs
@@ -916,16 +916,11 @@ impl DisplayListCommand for DrawCompositedContext {
pub struct DrawCanvas {
pub dst_rect: IntRect,
pub canvas_id: CanvasId,
- // NB: The canvas pixels live in the compositor's canvas surface registry, so the command bytes don't
- // change when the canvas content does. The content generation encodes content changes so that display
- // list damage computation can tell that the canvas needs to be repainted.
- pub content_generation: u64,
pub scaling_mode: ScalingMode,
}
ffi_bytes_fields!(DrawCanvas {
dst_rect,
canvas_id,
- content_generation,
scaling_mode
});
diff --git a/Libraries/LibCompositing/Rust/src/display_list/damage.rs b/Libraries/LibCompositing/Rust/src/display_list/damage.rs
index 6cbec778884..a4e00cd40b2 100644
--- a/Libraries/LibCompositing/Rust/src/display_list/damage.rs
+++ b/Libraries/LibCompositing/Rust/src/display_list/damage.rs
@@ -1868,11 +1868,10 @@ mod tests {
}
}
- fn canvas_command_bytes(rect: IntRect, content_generation: u64) -> TestDisplayList {
+ fn canvas_command_bytes(rect: IntRect) -> TestDisplayList {
let command = DrawCanvas {
dst_rect: rect,
canvas_id: CanvasId(1),
- content_generation,
scaling_mode: ScalingMode::NearestNeighbor,
};
command_bytes(&command, Some(rect), ContextRef::default())
@@ -2045,7 +2044,7 @@ mod tests {
fn changed_live_content_damages_where_it_is_drawn() {
let tree = identity_tree();
let rect = IntRect::new(10, 10, 20, 20);
- let canvas = canvas_command_bytes(rect, 0);
+ let canvas = canvas_command_bytes(rect);
let changed_canvas = LiveContentChanges {
canvases: &[CanvasId(1)],
..LiveContentChanges::default()
@@ -2124,7 +2123,6 @@ mod tests {
&DrawCanvas {
dst_rect: rect,
canvas_id: CanvasId(1),
- content_generation: 0,
scaling_mode: ScalingMode::NearestNeighbor,
},
Some(rect),
@@ -2144,7 +2142,7 @@ mod tests {
fn changed_live_content_inside_a_group_damages_the_whole_group() {
let tree = identity_tree();
let group_rect = IntRect::new(5, 5, 40, 40);
- let content = canvas_command_bytes(IntRect::new(10, 10, 20, 20), 0);
+ let content = canvas_command_bytes(IntRect::new(10, 10, 20, 20));
let no_data = DisplayListDataSpan { offset: 0, size: 0 };
let mut group = TestDisplayList::default();
append_record(
@@ -3357,7 +3355,6 @@ mod tests {
&DrawCanvas {
dst_rect: rect,
canvas_id: CanvasId(1),
- content_generation: 1,
scaling_mode: ScalingMode::NearestNeighbor,
},
Some(rect),
@@ -3533,20 +3530,9 @@ mod tests {
}
#[test]
- fn changed_canvas_content_generation_damages_canvas_rect() {
+ fn canvas_without_new_content_is_not_damaged() {
let tree = identity_tree();
- let old_display_list = canvas_command_bytes(IntRect::new(10, 10, 20, 20), 1);
- let new_display_list = canvas_command_bytes(IntRect::new(10, 10, 20, 20), 2);
- assert_eq!(
- damage(&old_display_list, &tree, &new_display_list, &tree),
- Some(IntRect::new(9, 9, 22, 22))
- );
- }
-
- #[test]
- fn unchanged_canvas_content_generation_does_not_damage_canvas() {
- let tree = identity_tree();
- let canvas = canvas_command_bytes(IntRect::new(10, 10, 20, 20), 1);
+ let canvas = canvas_command_bytes(IntRect::new(10, 10, 20, 20));
let old_fill = fill_command_bytes(IntRect::new(50, 50, 10, 10), RED);
let new_fill = fill_command_bytes(IntRect::new(50, 50, 10, 10), BLUE);
let mut old_display_list = canvas.clone();
diff --git a/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp b/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp
index a863acd1a2e..30b763fdbff 100644
--- a/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp
@@ -225,8 +225,7 @@ void HTMLCanvasElement::did_change_canvas_content()
}
// NB: Don't request a display list recording here: the new content reaches the compositor through the canvas
- // surface registry when the canvas is presented, and the cached DrawCanvas command is invalidated when the
- // content generation moves in prepare_for_compositing.
+ // surface registry when the canvas is presented.
set_needs_repaint(InvalidateDisplayList::No);
}
@@ -559,11 +558,6 @@ void HTMLCanvasElement::prepare_for_compositing()
if (m_canvas_content_dirty) {
m_canvas_content_dirty = false;
- // NB: The content generation is recorded into DrawCanvas display list commands, letting display list damage
- // computation see that the canvas content changed. Canvases are prepared for compositing before painting
- // in the rendering update, so display lists recorded in the same update pick up the new generation.
- ++m_content_generation;
-
m_context.visit(
[](GC::Ref<CanvasRenderingContext2D>& context) {
context->prepare_for_compositing();
diff --git a/Libraries/LibWeb/HTML/HTMLCanvasElement.h b/Libraries/LibWeb/HTML/HTMLCanvasElement.h
index 8011a7cb30d..56932c0379b 100644
--- a/Libraries/LibWeb/HTML/HTMLCanvasElement.h
+++ b/Libraries/LibWeb/HTML/HTMLCanvasElement.h
@@ -85,8 +85,6 @@ public:
Optional<Compositing::CanvasId> canvas_id() const;
- u64 content_generation() const { return m_content_generation; }
-
Optional<Gfx::IntSize> canvas_surface_content_size() const;
void ensure_backing_storage();
@@ -116,7 +114,6 @@ private:
Variant<GC::Ref<HTML::CanvasRenderingContext2D>, GC::Ref<WebGL::WebGLRenderingContext>, GC::Ref<WebGL::WebGL2RenderingContext>, GC::Ref<ImageBitmapRenderingContext>, Empty> m_context;
bool m_canvas_content_dirty { false };
- u64 m_content_generation { 0 };
bool m_is_placeholder { false };
Optional<Compositing::CanvasId> m_placeholder_canvas_id;
diff --git a/Libraries/LibWeb/Painting/PaintFacts.cpp b/Libraries/LibWeb/Painting/PaintFacts.cpp
index de18f882f1a..aa45ed621d7 100644
--- a/Libraries/LibWeb/Painting/PaintFacts.cpp
+++ b/Libraries/LibWeb/Painting/PaintFacts.cpp
@@ -82,7 +82,6 @@ static void queue_canvas_paint_facts(HTML::HTMLCanvasElement const& canvas)
facts.content_width = content_size->width();
facts.content_height = content_size->height();
facts.canvas_id = canvas.canvas_id().value().value();
- facts.content_generation = canvas.content_generation();
}
// Where the facts changed, the canvas's paint cache goes with them.
queue_element_paint_facts(canvas, [&](auto* host, u32 element) {
diff --git a/Libraries/LibWeb/Rust/src/painting/display_list/dump.rs b/Libraries/LibWeb/Rust/src/painting/display_list/dump.rs
index a6b5ac75965..8825f831fe5 100644
--- a/Libraries/LibWeb/Rust/src/painting/display_list/dump.rs
+++ b/Libraries/LibWeb/Rust/src/painting/display_list/dump.rs
@@ -447,7 +447,6 @@ fn dump_command(output: &mut String, command_type: DisplayListCommandType, paylo
DisplayListCommandType::DrawCanvas => {
let command = read_command::<DrawCanvas>(payload);
write_field(output, "dst_rect", command.dst_rect);
- write!(output, " content_generation={}", command.content_generation).unwrap();
}
DisplayListCommandType::DrawVideoFrame => {
let command = read_command::<DrawVideoFrame>(payload);
diff --git a/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rs b/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rs
index b50c8f034cb..b09b2a9c91b 100644
--- a/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rs
+++ b/Libraries/LibWeb/Rust/src/painting/display_list/recorder.rs
@@ -760,13 +760,7 @@ impl DisplayListRecorder {
);
}
- pub fn draw_canvas(
- &mut self,
- dst_rect: IntRect,
- canvas_id: CanvasId,
- content_generation: u64,
- scaling_mode: ScalingMode,
- ) {
+ pub fn draw_canvas(&mut self, dst_rect: IntRect, canvas_id: CanvasId, scaling_mode: ScalingMode) {
if dst_rect.is_empty() {
return;
}
@@ -774,7 +768,6 @@ impl DisplayListRecorder {
&DrawCanvas {
dst_rect,
canvas_id,
- content_generation,
scaling_mode,
},
&[],
diff --git a/Libraries/LibWeb/Rust/src/painting/host/paint.rs b/Libraries/LibWeb/Rust/src/painting/host/paint.rs
index da5f1052326..2578ae1063d 100644
--- a/Libraries/LibWeb/Rust/src/painting/host/paint.rs
+++ b/Libraries/LibWeb/Rust/src/painting/host/paint.rs
@@ -491,7 +491,6 @@ pub struct FfiCanvasPaintFacts {
pub content_width: i32,
pub content_height: i32,
pub canvas_id: u64,
- pub content_generation: u64,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/replaced.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/replaced.rs
index 9dae83a1355..d67f714144c 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/paint/replaced.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/replaced.rs
@@ -299,12 +299,9 @@ pub(crate) fn paint_canvas_foreground<O: Observer>(recorder: &mut PaintRecorder<
(facts.content_width, facts.content_height),
(canvas_rect.width, canvas_rect.height),
);
- recorder.recorder.draw_canvas(
- canvas_rect,
- CanvasId(facts.canvas_id),
- facts.content_generation,
- scaling_mode,
- );
+ recorder
+ .recorder
+ .draw_canvas(canvas_rect, CanvasId(facts.canvas_id), scaling_mode);
});
}
diff --git a/Libraries/LibWeb/Rust/src/render_state.rs b/Libraries/LibWeb/Rust/src/render_state.rs
index 530711d11e1..e50cb60625d 100644
--- a/Libraries/LibWeb/Rust/src/render_state.rs
+++ b/Libraries/LibWeb/Rust/src/render_state.rs
@@ -1141,9 +1141,9 @@ mod tests {
use crate::painting::replaced_paint_facts::ReplacedPaintFacts;
let element = MarkedBox::Node(StyleNodeID::from_raw(1));
let row = MarkedBox::Row(NodeSlotId::new(1, 1));
- let canvas = |content_generation| {
+ let canvas = |canvas_id| {
ReplacedPaintFacts::Canvas(FfiCanvasPaintFacts {
- content_generation,
+ canvas_id,
..Default::default()
})
};
diff --git a/Tests/LibWeb/Text/expected/canvas/2d-draw-does-not-record-display-list.txt b/Tests/LibWeb/Text/expected/canvas/2d-draw-does-not-record-display-list.txt
new file mode 100644
index 00000000000..a592d4a9f16
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/canvas/2d-draw-does-not-record-display-list.txt
@@ -0,0 +1 @@
+recordings after draw: 0
diff --git a/Tests/LibWeb/Text/expected/canvas/2d-draw-invalidates-cached-command.txt b/Tests/LibWeb/Text/expected/canvas/2d-draw-invalidates-cached-command.txt
deleted file mode 100644
index ad8b3f6e158..00000000000
--- a/Tests/LibWeb/Text/expected/canvas/2d-draw-invalidates-cached-command.txt
+++ /dev/null
@@ -1 +0,0 @@
-cached command has new generation: true
diff --git a/Tests/LibWeb/Text/expected/canvas/webgl-draw-does-not-record-display-list.txt b/Tests/LibWeb/Text/expected/canvas/webgl-draw-does-not-record-display-list.txt
new file mode 100644
index 00000000000..4fcd3f606e3
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/canvas/webgl-draw-does-not-record-display-list.txt
@@ -0,0 +1,2 @@
+[webgl] recordings after draw: 0
+[webgl2] recordings after draw: 0
diff --git a/Tests/LibWeb/Text/expected/canvas/webgl-draw-invalidates-cached-command.txt b/Tests/LibWeb/Text/expected/canvas/webgl-draw-invalidates-cached-command.txt
deleted file mode 100644
index 0eea4012e82..00000000000
--- a/Tests/LibWeb/Text/expected/canvas/webgl-draw-invalidates-cached-command.txt
+++ /dev/null
@@ -1,2 +0,0 @@
-[webgl] cached command has new generation: true
-[webgl2] cached command has new generation: true
diff --git a/Tests/LibWeb/Text/expected/display_list/canvas-compositor-surface.txt b/Tests/LibWeb/Text/expected/display_list/canvas-compositor-surface.txt
index 7a7b9dbdd91..0c6d167eb22 100644
--- a/Tests/LibWeb/Text/expected/display_list/canvas-compositor-surface.txt
+++ b/Tests/LibWeb/Text/expected/display_list/canvas-compositor-surface.txt
@@ -6,5 +6,5 @@ AccumulatedVisualContext Tree:
[e0 in s0] effects=[]
DisplayList:
-DrawCanvas@s1/e0 dst_rect=[8,8 32x32] content_generation=0
+DrawCanvas@s1/e0 dst_rect=[8,8 32x32]
diff --git a/Tests/LibWeb/Text/input/canvas/2d-draw-does-not-record-display-list.html b/Tests/LibWeb/Text/input/canvas/2d-draw-does-not-record-display-list.html
new file mode 100644
index 00000000000..dfc0862fc0d
--- /dev/null
+++ b/Tests/LibWeb/Text/input/canvas/2d-draw-does-not-record-display-list.html
@@ -0,0 +1,26 @@
+<!doctype html>
+<canvas id="canvas" width="100" height="100"></canvas>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const nextFrame = () => new Promise((resolve) => requestAnimationFrame(resolve));
+ const settle = async () => {
+ await nextFrame();
+ await nextFrame();
+ do await new Promise((resolve) => setTimeout(resolve));
+ while (internals.frameSchedulerState() !== "idle");
+ };
+ const recordingCount = () => (internals.takeDisplayListTrace().match(/^recording /gm) ?? []).length;
+
+ const context = canvas.getContext("2d");
+ context.fillStyle = "red";
+ context.fillRect(0, 0, canvas.width, canvas.height);
+ await settle();
+
+ internals.beginDisplayListTrace();
+ context.fillStyle = "green";
+ context.fillRect(0, 0, canvas.width, canvas.height);
+ await settle();
+ println(`recordings after draw: ${recordingCount()}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/canvas/2d-draw-invalidates-cached-command.html b/Tests/LibWeb/Text/input/canvas/2d-draw-invalidates-cached-command.html
deleted file mode 100644
index f414cd687b7..00000000000
--- a/Tests/LibWeb/Text/input/canvas/2d-draw-invalidates-cached-command.html
+++ /dev/null
@@ -1,30 +0,0 @@
-<!doctype html>
-<canvas id="canvas" width="100" height="100"></canvas>
-<script src="../include.js"></script>
-<script>
- promiseTest(async () => {
- const nextFrame = () => new Promise((resolve) => requestAnimationFrame(resolve));
- const canvasContentGeneration = () => {
- const match = internals.dumpDisplayList().match(/DrawCanvas@\S+ .*content_generation=(\d+)/);
- return Number(match[1]);
- };
-
- const context = canvas.getContext("2d");
- context.fillStyle = "red";
- context.fillRect(0, 0, canvas.width, canvas.height);
- await nextFrame();
- await nextFrame();
- const generationBeforeDraw = canvasContentGeneration();
-
- context.fillStyle = "green";
- context.fillRect(0, 0, canvas.width, canvas.height);
-
- // Trigger an unrelated display list recording in the same rendering update. It must not
- // reuse the DrawCanvas command cached before the canvas content update.
- document.body.style.backgroundColor = "white";
- await nextFrame();
-
- const generationAfterDraw = canvasContentGeneration();
- println("cached command has new generation: " + (generationAfterDraw > generationBeforeDraw));
- });
-</script>
diff --git a/Tests/LibWeb/Text/input/canvas/webgl-draw-does-not-invalidate-display-list.html b/Tests/LibWeb/Text/input/canvas/webgl-draw-does-not-invalidate-display-list.html
index dde11f15e1a..2437ea0f37b 100644
--- a/Tests/LibWeb/Text/input/canvas/webgl-draw-does-not-invalidate-display-list.html
+++ b/Tests/LibWeb/Text/input/canvas/webgl-draw-does-not-invalidate-display-list.html
@@ -4,9 +4,7 @@
<script src="../include.js"></script>
<script>
// WebGL canvas content updates reach the compositor through the canvas surface registry,
- // so a draw call must schedule a repaint without requesting a display list recording. The
- // cached DrawCanvas command is invalidated separately, so any recording requested by another
- // change will include the new content generation and damage the canvas.
+ // so a draw call must schedule a repaint without requesting a display list recording.
//
// Creating a WebGL context is the exception: it must invalidate the display list, since the
// recorded DrawCanvas command refers to the context's canvas id.
diff --git a/Tests/LibWeb/Text/input/canvas/webgl-draw-does-not-record-display-list.html b/Tests/LibWeb/Text/input/canvas/webgl-draw-does-not-record-display-list.html
new file mode 100644
index 00000000000..4043742eeeb
--- /dev/null
+++ b/Tests/LibWeb/Text/input/canvas/webgl-draw-does-not-record-display-list.html
@@ -0,0 +1,35 @@
+<!doctype html>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ const nextFrame = () => new Promise((resolve) => requestAnimationFrame(resolve));
+ const settle = async () => {
+ await nextFrame();
+ await nextFrame();
+ do await new Promise((resolve) => setTimeout(resolve));
+ while (internals.frameSchedulerState() !== "idle");
+ };
+ const recordingCount = () => (internals.takeDisplayListTrace().match(/^recording /gm) ?? []).length;
+
+ for (const contextType of ["webgl", "webgl2"]) {
+ const canvas = document.createElement("canvas");
+ canvas.width = 100;
+ canvas.height = 100;
+ document.body.appendChild(canvas);
+
+ const gl = canvas.getContext(contextType);
+ gl.clearColor(1, 0, 0, 1);
+ gl.clear(gl.COLOR_BUFFER_BIT);
+ await settle();
+
+ internals.beginDisplayListTrace();
+ gl.clearColor(0, 1, 0, 1);
+ gl.clear(gl.COLOR_BUFFER_BIT);
+ await settle();
+ println(`[${contextType}] recordings after draw: ${recordingCount()}`);
+
+ canvas.remove();
+ await settle();
+ }
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/canvas/webgl-draw-invalidates-cached-command.html b/Tests/LibWeb/Text/input/canvas/webgl-draw-invalidates-cached-command.html
deleted file mode 100644
index bee50fab11f..00000000000
--- a/Tests/LibWeb/Text/input/canvas/webgl-draw-invalidates-cached-command.html
+++ /dev/null
@@ -1,36 +0,0 @@
-<!doctype html>
-<script src="../include.js"></script>
-<script>
- promiseTest(async () => {
- const nextFrame = () => new Promise((resolve) => requestAnimationFrame(resolve));
- const canvasContentGeneration = () => {
- const match = internals.dumpDisplayList().match(/DrawCanvas@\S+ .*content_generation=(\d+)/);
- return Number(match[1]);
- };
-
- for (const [contextType, backgroundColor] of [["webgl", "white"], ["webgl2", "black"]]) {
- const canvas = document.createElement("canvas");
- canvas.width = 100;
- canvas.height = 100;
- document.body.appendChild(canvas);
-
- const gl = canvas.getContext(contextType);
- await nextFrame();
- const generationBeforeDraw = canvasContentGeneration();
-
- gl.clearColor(0, 1, 0, 1);
- gl.clear(gl.COLOR_BUFFER_BIT);
-
- // Trigger an unrelated display list recording in the same rendering update. It must not
- // reuse the DrawCanvas command cached before the WebGL content update.
- document.body.style.backgroundColor = backgroundColor;
- await nextFrame();
-
- const generationAfterDraw = canvasContentGeneration();
- println(`[${contextType}] cached command has new generation: ${generationAfterDraw > generationBeforeDraw}`);
-
- canvas.remove();
- await nextFrame();
- }
- });
-</script>