Skip to content

Truly independent web browser

UI/Qt: Skip painting the web view while the page layer covers it

Aliaksandr Kalenikde5cef5a4d10
diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cpp
index ceadaacf22d..92ba01d91b1 100644
--- a/UI/Qt/WebContentView.cpp
+++ b/UI/Qt/WebContentView.cpp
@@ -134,7 +134,8 @@ WebContentView::WebContentView(QWidget* window, Optional<WebView::CanonicalTrave
setParent(window);
setApi(QRhiWidget::Api::Direct3D11);
#elif defined(LADYBIRD_QT_USE_IOSURFACE_LAYER)
- // The page is shown by a layer of this widget's own native view; Qt only paints the chrome behind its corners.
+ // The page is shown by a layer of this widget's own native view, and Qt paints the widget only while that layer does
+ // not cover it (see update_whether_qt_paints_view()).
setAttribute(Qt::WA_DontCreateNativeAncestors);
setAttribute(Qt::WA_NativeWindow);
setAttribute(Qt::WA_OpaquePaintEvent);
@@ -1276,6 +1277,9 @@ void WebContentView::paintEvent(QPaintEvent*)
#ifdef LADYBIRD_QT_USE_IOSURFACE_LAYER
void WebContentView::paintEvent(QPaintEvent*)
{
+ // Qt still sends paint events to a view that paints on screen, which has no paint engine to paint with.
+ if (testAttribute(Qt::WA_PaintOnScreen))
+ return;
// The page layer covers the view, except before its first frame, which shows the chrome's color.
QPainter painter(this);
painter.fillRect(rect(), ChromeStyle::chrome_background(window()->palette()));
diff --git a/UI/Qt/WebContentView.h b/UI/Qt/WebContentView.h
index b8eada9af86..d3ae5592776 100644
--- a/UI/Qt/WebContentView.h
+++ b/UI/Qt/WebContentView.h
@@ -364,6 +364,9 @@ private:
void update_iosurface_layer_background_color();
void detach_iosurface_layer_from_native_view();
void destroy_iosurface_layer();
+ void update_whether_qt_paints_view();
+ virtual QPaintEngine* paintEngine() const override;
+ virtual void childEvent(QChildEvent*) override;
void* m_iosurface_layer { nullptr };
void* m_page_dim_layer { nullptr };
diff --git a/UI/Qt/WebContentViewMac.mm b/UI/Qt/WebContentViewMac.mm
index 251e227968a..5d16f32818a 100644
--- a/UI/Qt/WebContentViewMac.mm
+++ b/UI/Qt/WebContentViewMac.mm
@@ -4,9 +4,11 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/ScopeGuard.h>
#include <LibGfx/SharedImageBuffer.h>
#include <UI/Qt/WebContentView.h>
+#include <QChildEvent>
#include <QWindow>
#include <qpa/qplatformwindow_p.h>
@@ -60,6 +62,7 @@ bool WebContentView::ensure_iosurface_layer_attached_to_native_view()
update_iosurface_layer_background_color();
// Overlays are child widgets that Qt paints into the content layer, underneath this one.
layer.hidden = is_page_covered_by_overlay();
+ update_whether_qt_paints_view();
return true;
}
@@ -67,6 +70,7 @@ void WebContentView::present_current_paintable_as_layer_contents()
{
if (!ensure_iosurface_layer_attached_to_native_view())
return;
+ ScopeGuard update_painting_for_new_contents = [this] { update_whether_qt_paints_view(); };
auto* layer = as_layer(m_iosurface_layer);
layer.contentsScale = m_device_pixel_ratio;
@@ -163,6 +167,44 @@ void WebContentView::detach_iosurface_layer_from_native_view()
{
if (m_iosurface_layer)
[as_layer(m_iosurface_layer) removeFromSuperlayer];
+ update_whether_qt_paints_view();
+}
+
+// While the page layer covers the view, the view paints on screen, so Qt neither paints it into the window's backing
+// store nor copies it into a backing store of the view's own native view. All of that would lie under the layer, and
+// on every step of a window resize it is a fill and a copy of the whole view into freshly allocated surfaces. Qt paints
+// the view before the layer has a frame, and while an overlay, which Qt paints into the view, hides the layer.
+void WebContentView::update_whether_qt_paints_view()
+{
+ auto* layer = as_layer(m_iosurface_layer);
+ bool page_layer_covers_view = layer.superlayer != nil && !layer.hidden && layer.contents != nil;
+ if (testAttribute(Qt::WA_PaintOnScreen) == page_layer_covers_view)
+ return;
+ setAttribute(Qt::WA_PaintOnScreen, page_layer_covers_view);
+ // What Qt last painted of the view is from before the layer covered it. Painting now puts the view as it is into
+ // the same window update in which the layer stops covering it.
+ if (!page_layer_covers_view)
+ repaint();
+}
+
+// The view paints nothing on screen. QWidget's own version returns no engine either, but warns.
+QPaintEngine* WebContentView::paintEngine() const
+{
+ return nullptr;
+}
+
+// Qt gives each child of a widget that paints on screen a native view of its own, which would stack the overlays and
+// the dialog above the page layer, showing whatever lies under them in the window's backing store. They stay painted
+// into this view's native view instead, as while Qt paints the view. A child that wants a native view asks for one in
+// its own constructor, after this.
+void WebContentView::childEvent(QChildEvent* event)
+{
+ WebContentViewBase::childEvent(event);
+ if (!event->added() || !testAttribute(Qt::WA_PaintOnScreen) || !event->child()->isWidgetType())
+ return;
+ auto* child = static_cast<QWidget*>(event->child());
+ if (!child->isWindow() && !child->testAttribute(Qt::WA_WState_Created))
+ child->setAttribute(Qt::WA_NativeWindow, false);
}
void WebContentView::destroy_iosurface_layer()