Skip to content

Truly independent web browser

LibWeb: Present 2D canvases only once they have a box

Aliaksandr Kalenika27cc27af399
diff --git a/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp b/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp
index ff4c234f25d..dd09bf6e054 100644
--- a/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLCanvasElement.cpp
@@ -501,6 +501,13 @@ void HTMLCanvasElement::set_canvas_content_dirty()
void HTMLCanvasElement::prepare_for_compositing()
{
+ // OPTIMIZATION: Nothing shows a 2D canvas that has no box, and its readbacks read what it drew rather than what it
+ // presented, so what it draws stays unpresented until it has a box. That spares the Compositor a
+ // copy of every frame it draws, which for a canvas that draws in memory is an upload. The rendering
+ // update lays out before it prepares canvases, so a canvas presents in the update that gives it a box.
+ if (canvas_rendering_context_2d() && !has_layout_box())
+ return;
+
if (m_canvas_content_dirty) {
m_canvas_content_dirty = false;
diff --git a/Libraries/LibWeb/HTML/HTMLCanvasElement.h b/Libraries/LibWeb/HTML/HTMLCanvasElement.h
index 8bb4c9d565b..f712af68f30 100644
--- a/Libraries/LibWeb/HTML/HTMLCanvasElement.h
+++ b/Libraries/LibWeb/HTML/HTMLCanvasElement.h
@@ -72,6 +72,7 @@ public:
WebIDL::ExceptionOr<void> to_blob(GC::Ref<WebIDL::CallbackType> callback, Utf16View type, Optional<JS::Value> quality);
void prepare_for_compositing();
+ bool has_unpresented_content() const { return m_canvas_content_dirty; }
void notify_compositor_backing_storage_lost();
void set_canvas_content_dirty();
GC::Ptr<HTML::CanvasRenderingContext2D> canvas_rendering_context_2d() const
diff --git a/Libraries/LibWeb/Internals/Internals.cpp b/Libraries/LibWeb/Internals/Internals.cpp
index f686dc53ee1..2d86194e9cd 100644
--- a/Libraries/LibWeb/Internals/Internals.cpp
+++ b/Libraries/LibWeb/Internals/Internals.cpp
@@ -69,6 +69,7 @@
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/EventLoop/TaskQueue.h>
#include <LibWeb/HTML/FormAssociatedElement.h>
+#include <LibWeb/HTML/HTMLCanvasElement.h>
#include <LibWeb/HTML/HTMLElement.h>
#include <LibWeb/HTML/HTMLIFrameElement.h>
#include <LibWeb/HTML/HTMLInputElement.h>
@@ -1838,6 +1839,11 @@ bool Internals::has_populated_document(HTML::HTMLIFrameElement& iframe)
return window().associated_document().page().client().page_did_request_has_populated_document_for_testing(navigable->id());
}
+bool Internals::canvas_has_unpresented_content(HTML::HTMLCanvasElement& canvas)
+{
+ return canvas.has_unpresented_content();
+}
+
Utf16String Internals::dump_site_isolation_process_tree()
{
return dump_string_to_utf16(window().associated_document().page().client().dump_site_isolation_process_tree_for_testing());
diff --git a/Libraries/LibWeb/Internals/Internals.h b/Libraries/LibWeb/Internals/Internals.h
index fa5984795ce..da7389c9e8a 100644
--- a/Libraries/LibWeb/Internals/Internals.h
+++ b/Libraries/LibWeb/Internals/Internals.h
@@ -218,6 +218,7 @@ public:
Utf16String dump_session_store_tab_state();
Utf16String dump_site_isolation_process_tree();
bool has_populated_document(HTML::HTMLIFrameElement&);
+ bool canvas_has_unpresented_content(HTML::HTMLCanvasElement&);
void crash_remote_frame_processes();
void stop_loading_through_ui_process();
void reload_through_ui_process();
diff --git a/Libraries/LibWeb/Internals/Internals.idl b/Libraries/LibWeb/Internals/Internals.idl
index a06fda1e23a..175767cf3e2 100644
--- a/Libraries/LibWeb/Internals/Internals.idl
+++ b/Libraries/LibWeb/Internals/Internals.idl
@@ -216,6 +216,8 @@ interface Internals {
DOMString dumpSiteIsolationProcessTree();
// Reports if the browser process has created a document for the iframe that it hasn't made active yet.
boolean hasPopulatedDocument(HTMLIFrameElement iframe);
+ // Whether the canvas drew content that it has not presented to the compositor yet.
+ boolean canvasHasUnpresentedContent(HTMLCanvasElement canvas);
undefined crashRemoteFrameProcesses();
// Stops loading and reloads as the browser's Stop and Reload buttons do.
undefined stopLoadingThroughUIProcess();
diff --git a/Tests/LibWeb/Text/expected/canvas/2d-presents-content-once-it-gets-a-box.txt b/Tests/LibWeb/Text/expected/canvas/2d-presents-content-once-it-gets-a-box.txt
new file mode 100644
index 00000000000..74cc883b11d
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/canvas/2d-presents-content-once-it-gets-a-box.txt
@@ -0,0 +1,4 @@
+Detached: true
+Inserted: false
+Drawn while display: none: true
+Displayed again: false
diff --git a/Tests/LibWeb/Text/input/canvas/2d-presents-content-once-it-gets-a-box.html b/Tests/LibWeb/Text/input/canvas/2d-presents-content-once-it-gets-a-box.html
new file mode 100644
index 00000000000..fca87ae1e86
--- /dev/null
+++ b/Tests/LibWeb/Text/input/canvas/2d-presents-content-once-it-gets-a-box.html
@@ -0,0 +1,35 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ function nextRenderingUpdate() {
+ return new Promise(resolve => requestAnimationFrame(() => resolve()));
+ }
+
+ asyncTest(async done => {
+ const canvas = document.createElement("canvas");
+ canvas.width = 10;
+ canvas.height = 10;
+ const context = canvas.getContext("2d");
+ context.fillRect(0, 0, 10, 10);
+
+ await nextRenderingUpdate();
+ await nextRenderingUpdate();
+ println(`Detached: ${internals.canvasHasUnpresentedContent(canvas)}`);
+
+ document.body.appendChild(canvas);
+ await nextRenderingUpdate();
+ println(`Inserted: ${internals.canvasHasUnpresentedContent(canvas)}`);
+
+ canvas.style.display = "none";
+ await nextRenderingUpdate();
+ context.fillRect(0, 0, 5, 5);
+ await nextRenderingUpdate();
+ println(`Drawn while display: none: ${internals.canvasHasUnpresentedContent(canvas)}`);
+
+ canvas.style.display = "";
+ await nextRenderingUpdate();
+ println(`Displayed again: ${internals.canvasHasUnpresentedContent(canvas)}`);
+
+ done();
+ });
+</script>