Skip to content

Commit

Permalink
[GTK][WPE] Prefer opaque buffer formats for DMABuf renderer if page b…
Browse files Browse the repository at this point in the history
…ackground color is opaque

https://bugs.webkit.org/show_bug.cgi?id=270964

Reviewed by Alejandro G. Castro.

And make sure to not clear every frame with transparent color when the
page background is opaque.

* Source/WebCore/platform/graphics/PlatformDisplay.cpp:
(WebCore::PlatformDisplay::dmabufFormats):
* Source/WebKit/Shared/CoordinatedGraphics/threadedcompositor/ThreadedCompositor.cpp:
(WebKit::ThreadedCompositor::renderLayerTree):
* Source/WebKit/Shared/CoordinatedGraphics/threadedcompositor/ThreadedCompositor.h:
* Source/WebKit/Shared/glib/DMABufRendererBufferFormat.h:
(WebKit::DMABufRendererBufferFormat::operator== const):
* Source/WebKit/WebProcess/WebPage/AcceleratedSurface.cpp:
(WebKit::AcceleratedSurface::AcceleratedSurface):
(WebKit::AcceleratedSurface::backgroundColorDidChange):
(WebKit::AcceleratedSurface::clearIfNeeded):
* Source/WebKit/WebProcess/WebPage/AcceleratedSurface.h:
* Source/WebKit/WebProcess/WebPage/CoordinatedGraphics/DrawingAreaCoordinatedGraphics.cpp:
(WebKit::DrawingAreaCoordinatedGraphics::backgroundColorDidChange):
* Source/WebKit/WebProcess/WebPage/CoordinatedGraphics/DrawingAreaCoordinatedGraphics.h:
* Source/WebKit/WebProcess/WebPage/CoordinatedGraphics/LayerTreeHost.cpp:
(WebKit::LayerTreeHost::backgroundColorDidChange):
(WebKit::LayerTreeHost::clearIfNeeded):
* Source/WebKit/WebProcess/WebPage/CoordinatedGraphics/LayerTreeHost.h:
* Source/WebKit/WebProcess/WebPage/CoordinatedGraphics/LayerTreeHostTextureMapper.cpp:
(WebKit::LayerTreeHost::backgroundColorDidChange):
* Source/WebKit/WebProcess/WebPage/CoordinatedGraphics/LayerTreeHostTextureMapper.h:
* Source/WebKit/WebProcess/WebPage/DrawingArea.h:
* Source/WebKit/WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::setBackgroundColor):
* Source/WebKit/WebProcess/WebPage/dmabuf/AcceleratedSurfaceDMABuf.cpp:
(WebKit::AcceleratedSurfaceDMABuf::AcceleratedSurfaceDMABuf):
(WebKit::AcceleratedSurfaceDMABuf::SwapChain::setupBufferFormat):
(WebKit::AcceleratedSurfaceDMABuf::preferredBufferFormatsDidChange):
(WebKit::AcceleratedSurfaceDMABuf::backgroundColorDidChange):
* Source/WebKit/WebProcess/WebPage/dmabuf/AcceleratedSurfaceDMABuf.h:

Canonical link: https://commits.webkit.org/276146@main
  • Loading branch information
carlosgcampos committed Mar 15, 2024
1 parent d1f63c0 commit 53d402b
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 19 deletions.
6 changes: 3 additions & 3 deletions Source/WebCore/platform/graphics/PlatformDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,11 +577,11 @@ const Vector<PlatformDisplay::DMABufFormat>& PlatformDisplay::dmabufFormats()

// For now we only support formats that can be created with a single GBM buffer for all planes.
static const Vector<EGLint> s_supportedFormats = {
DRM_FORMAT_ARGB8888, DRM_FORMAT_RGBA8888, DRM_FORMAT_ABGR8888, DRM_FORMAT_BGRA8888,
DRM_FORMAT_XRGB8888, DRM_FORMAT_RGBX8888, DRM_FORMAT_XBGR8888, DRM_FORMAT_BGRX8888,
DRM_FORMAT_ARGB8888, DRM_FORMAT_RGBA8888, DRM_FORMAT_ABGR8888, DRM_FORMAT_BGRA8888,
DRM_FORMAT_RGB565,
DRM_FORMAT_ARGB2101010, DRM_FORMAT_ABGR2101010, DRM_FORMAT_XRGB2101010, DRM_FORMAT_XBGR2101010,
DRM_FORMAT_ARGB16161616F, DRM_FORMAT_ABGR16161616F, DRM_FORMAT_XRGB16161616F, DRM_FORMAT_XBGR16161616F
DRM_FORMAT_XRGB2101010, DRM_FORMAT_XBGR2101010, DRM_FORMAT_ARGB2101010, DRM_FORMAT_ABGR2101010,
DRM_FORMAT_XRGB16161616F, DRM_FORMAT_XBGR16161616F, DRM_FORMAT_ARGB16161616F, DRM_FORMAT_ABGR16161616F
};

m_dmabufFormats = WTF::compactMap(s_supportedFormats, [&](auto format) -> std::optional<DMABufFormat> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,7 @@ void ThreadedCompositor::renderLayerTree()
if (needsResize)
glViewport(0, 0, viewportSize.width(), viewportSize.height());

glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
m_client.clearIfNeeded();

m_scene->applyStateChanges(states);
m_scene->paintToCurrentGLContext(viewportTransform, FloatRect { FloatPoint { }, viewportSize }, m_flipY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class ThreadedCompositor : public CoordinatedGraphicsSceneClient, public ThreadS

virtual void resize(const WebCore::IntSize&) = 0;
virtual void willRenderFrame() = 0;
virtual void clearIfNeeded() = 0;
virtual void didRenderFrame() = 0;
virtual void displayDidRefresh(WebCore::PlatformDisplayID) = 0;
};
Expand Down
5 changes: 5 additions & 0 deletions Source/WebKit/Shared/glib/DMABufRendererBufferFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ enum class DMABufRendererBufferFormatUsage : uint8_t {
};

struct DMABufRendererBufferFormat {
bool operator==(const DMABufRendererBufferFormat& other) const
{
return usage == other.usage && fourcc == other.fourcc && modifiers == other.modifiers;
}

using Usage = DMABufRendererBufferFormatUsage;
Usage usage { Usage::Rendering };
uint32_t fourcc { 0 };
Expand Down
27 changes: 27 additions & 0 deletions Source/WebKit/WebProcess/WebPage/AcceleratedSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
#include "AcceleratedSurfaceDMABuf.h"
#endif

#if USE(LIBEPOXY)
#include <epoxy/gl.h>
#else
#include <GLES2/gl2.h>
#endif

namespace WebKit {
using namespace WebCore;

Expand All @@ -62,6 +68,7 @@ AcceleratedSurface::AcceleratedSurface(WebPage& webPage, Client& client)
: m_webPage(webPage)
, m_client(client)
, m_size(webPage.size())
, m_isOpaque(!webPage.backgroundColor().has_value() || webPage.backgroundColor()->isOpaque())
{
m_size.scale(m_webPage.deviceScaleFactor());
}
Expand All @@ -77,4 +84,24 @@ bool AcceleratedSurface::hostResize(const IntSize& size)
return true;
}

bool AcceleratedSurface::backgroundColorDidChange()
{
const auto& color = m_webPage.backgroundColor();
auto isOpaque = !color.has_value() || color->isOpaque();
if (m_isOpaque == isOpaque)
return false;

m_isOpaque = isOpaque;
return true;
}

void AcceleratedSurface::clearIfNeeded()
{
if (m_isOpaque)
return;

glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
}

} // namespace WebKit
4 changes: 4 additions & 0 deletions Source/WebKit/WebProcess/WebPage/AcceleratedSurface.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,17 @@ class AcceleratedSurface {
#endif

virtual void visibilityDidChange(bool) { }
virtual bool backgroundColorDidChange();

void clearIfNeeded();

protected:
AcceleratedSurface(WebPage&, Client&);

WebPage& m_webPage;
Client& m_client;
WebCore::IntSize m_size;
std::atomic<bool> m_isOpaque { true };
};

} // namespace WebKit
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,12 @@ bool DrawingAreaCoordinatedGraphics::enterAcceleratedCompositingModeIfNeeded()
enterAcceleratedCompositingMode(nullptr);
return true;
}

void DrawingAreaCoordinatedGraphics::backgroundColorDidChange()
{
if (m_layerTreeHost)
m_layerTreeHost->backgroundColorDidChange();
}
#endif

void DrawingAreaCoordinatedGraphics::setDeviceScaleFactor(float deviceScaleFactor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class DrawingAreaCoordinatedGraphics final : public DrawingArea {
void deviceOrPageScaleFactorChanged() override;
void didChangeViewportAttributes(WebCore::ViewportAttributes&&) override;
bool enterAcceleratedCompositingModeIfNeeded() override;
void backgroundColorDidChange() override;
#endif

#if PLATFORM(WPE) && USE(GBM) && ENABLE(WPE_PLATFORM)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ void LayerTreeHost::deviceOrPageScaleFactorChanged()
didChangeViewport();
}

void LayerTreeHost::backgroundColorDidChange()
{
m_surface->backgroundColorDidChange();
}

#if !HAVE(DISPLAY_LINK)
RefPtr<DisplayRefreshMonitor> LayerTreeHost::createDisplayRefreshMonitor(PlatformDisplayID displayID)
{
Expand Down Expand Up @@ -399,6 +404,11 @@ void LayerTreeHost::willRenderFrame()
m_surface->willRenderFrame();
}

void LayerTreeHost::clearIfNeeded()
{
m_surface->clearIfNeeded();
}

void LayerTreeHost::didRenderFrame()
{
m_surface->didRenderFrame();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class LayerTreeHost
void didChangeViewportAttributes(WebCore::ViewportAttributes&&);

void deviceOrPageScaleFactorChanged();
void backgroundColorDidChange();

#if !HAVE(DISPLAY_LINK)
RefPtr<WebCore::DisplayRefreshMonitor> createDisplayRefreshMonitor(WebCore::PlatformDisplayID);
Expand Down Expand Up @@ -138,6 +139,7 @@ class LayerTreeHost
void didDestroyGLContext() override;
void resize(const WebCore::IntSize&) override;
void willRenderFrame() override;
void clearIfNeeded() override;
void didRenderFrame() override;
void displayDidRefresh(WebCore::PlatformDisplayID) override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ void LayerTreeHost::deviceOrPageScaleFactorChanged()
{
}

void LayerTreeHost::backgroundColorDidChange()
{
}

RefPtr<WebCore::DisplayRefreshMonitor> LayerTreeHost::createDisplayRefreshMonitor(WebCore::PlatformDisplayID)
{
return nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class LayerTreeHost : public WebCore::GraphicsLayerClient {
void didChangeViewportAttributes(WebCore::ViewportAttributes&&);
void setIsDiscardable(bool);
void deviceOrPageScaleFactorChanged();
void backgroundColorDidChange();
RefPtr<WebCore::DisplayRefreshMonitor> createDisplayRefreshMonitor(WebCore::PlatformDisplayID);
WebCore::PlatformDisplayID displayID() const { return m_displayID; }

Expand Down
1 change: 1 addition & 0 deletions Source/WebKit/WebProcess/WebPage/DrawingArea.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class DrawingArea : public IPC::MessageReceiver, public WebCore::DisplayRefreshM
virtual void didChangeViewportAttributes(WebCore::ViewportAttributes&&) = 0;
virtual void deviceOrPageScaleFactorChanged() = 0;
virtual bool enterAcceleratedCompositingModeIfNeeded() = 0;
virtual void backgroundColorDidChange() { };
#endif

#if PLATFORM(WPE) && USE(GBM) && ENABLE(WPE_PLATFORM)
Expand Down
3 changes: 3 additions & 0 deletions Source/WebKit/WebProcess/WebPage/WebPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3921,6 +3921,9 @@ void WebPage::setBackgroundColor(const std::optional<WebCore::Color>& background
if (RefPtr frameView = localMainFrameView())
frameView->updateBackgroundRecursively(backgroundColor);

#if USE(COORDINATED_GRAPHICS) || USE(TEXTURE_MAPPER)
m_drawingArea->backgroundColorDidChange();
#endif
m_drawingArea->setNeedsDisplay();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ AcceleratedSurfaceDMABuf::AcceleratedSurfaceDMABuf(WebPage& webPage, Client& cli
{
#if USE(GBM)
if (m_swapChain.type() == SwapChain::Type::EGLImage)
m_swapChain.setupBufferFormat(m_webPage.preferredBufferFormats());
m_swapChain.setupBufferFormat(m_webPage.preferredBufferFormats(), m_isOpaque);
#endif
}

Expand Down Expand Up @@ -362,7 +362,7 @@ AcceleratedSurfaceDMABuf::SwapChain::SwapChain(uint64_t surfaceID)
}

#if USE(GBM)
void AcceleratedSurfaceDMABuf::SwapChain::setupBufferFormat(const Vector<DMABufRendererBufferFormat>& preferredFormats)
void AcceleratedSurfaceDMABuf::SwapChain::setupBufferFormat(const Vector<DMABufRendererBufferFormat>& preferredFormats, bool isOpaque)
{
// The preferred formats vector is sorted by usage, but all formats for the same usage has the same priority.
// We split the preferred formats by usage to find in them separately.
Expand Down Expand Up @@ -391,29 +391,56 @@ void AcceleratedSurfaceDMABuf::SwapChain::setupBufferFormat(const Vector<DMABufR
return notFound;
};

auto isOpaqueFormat = [](uint32_t fourcc) -> bool {
return fourcc != DRM_FORMAT_ARGB8888
&& fourcc != DRM_FORMAT_RGBA8888
&& fourcc != DRM_FORMAT_ABGR8888
&& fourcc != DRM_FORMAT_BGRA8888
&& fourcc != DRM_FORMAT_ARGB2101010
&& fourcc != DRM_FORMAT_ABGR2101010
&& fourcc != DRM_FORMAT_ARGB16161616F
&& fourcc != DRM_FORMAT_ABGR16161616F;
};

Locker locker { m_dmabufFormatLock };
DMABufRendererBufferFormat dmabufFormat;
const auto& supportedFormats = WebCore::PlatformDisplay::sharedDisplayForCompositing().dmabufFormats();
for (const auto& format : supportedFormats) {
for (const auto& tranche : tranches) {
for (const auto& tranche : tranches) {
for (const auto& format : supportedFormats) {
auto index = findInRange(format.fourcc, tranche);
if (index != notFound) {
const auto& dmabufFormat = preferredFormats[index];
m_dmabufFormat.usage = dmabufFormat.usage;
m_dmabufFormat.fourcc = dmabufFormat.fourcc;
if (dmabufFormat.modifiers[0] == DRM_FORMAT_MOD_INVALID)
m_dmabufFormat.modifiers = dmabufFormat.modifiers;
const auto& preferredFormat = preferredFormats[index];

bool matchesOpacity = isOpaqueFormat(preferredFormat.fourcc) == isOpaque;
if (!matchesOpacity && dmabufFormat.fourcc)
continue;

dmabufFormat.usage = preferredFormat.usage;
dmabufFormat.fourcc = preferredFormat.fourcc;
if (preferredFormat.modifiers[0] == DRM_FORMAT_MOD_INVALID)
dmabufFormat.modifiers = preferredFormat.modifiers;
else {
m_dmabufFormat.modifiers = WTF::compactMap(dmabufFormat.modifiers, [&format](uint64_t modifier) -> std::optional<uint64_t> {
dmabufFormat.modifiers = WTF::compactMap(preferredFormat.modifiers, [&format](uint64_t modifier) -> std::optional<uint64_t> {
if (format.modifiers.contains(modifier))
return modifier;
return std::nullopt;
});
}
m_dmabufFormatChanged = true;
return;

if (matchesOpacity)
break;
}
}

if (dmabufFormat.fourcc)
break;
}

if (!dmabufFormat.fourcc || dmabufFormat == m_dmabufFormat)
return;

m_dmabufFormat = WTFMove(dmabufFormat);
m_dmabufFormatChanged = true;
}
#endif

Expand Down Expand Up @@ -492,7 +519,7 @@ void AcceleratedSurfaceDMABuf::preferredBufferFormatsDidChange()
if (m_swapChain.type() != SwapChain::Type::EGLImage)
return;

m_swapChain.setupBufferFormat(m_webPage.preferredBufferFormats());
m_swapChain.setupBufferFormat(m_webPage.preferredBufferFormats(), m_isOpaque);
}
#endif

Expand All @@ -513,6 +540,17 @@ void AcceleratedSurfaceDMABuf::visibilityDidChange(bool isVisible)
}
}

bool AcceleratedSurfaceDMABuf::backgroundColorDidChange()
{
if (!AcceleratedSurface::backgroundColorDidChange())
return false;

if (m_swapChain.type() == SwapChain::Type::EGLImage)
m_swapChain.setupBufferFormat(m_webPage.preferredBufferFormats(), m_isOpaque);

return true;
}

void AcceleratedSurfaceDMABuf::releaseUnusedBuffersTimerFired()
{
m_swapChain.releaseUnusedBuffers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class AcceleratedSurfaceDMABuf final : public AcceleratedSurface, public IPC::Me
#endif

void visibilityDidChange(bool) override;
bool backgroundColorDidChange() override;

AcceleratedSurfaceDMABuf(WebPage&, Client&);

Expand Down Expand Up @@ -173,7 +174,7 @@ class AcceleratedSurfaceDMABuf final : public AcceleratedSurface, public IPC::Me
unsigned size() const { return m_freeTargets.size() + m_lockedTargets.size(); }

#if USE(GBM)
void setupBufferFormat(const Vector<DMABufRendererBufferFormat>&);
void setupBufferFormat(const Vector<DMABufRendererBufferFormat>&, bool);
#endif

private:
Expand Down

0 comments on commit 53d402b

Please sign in to comment.